leftso 652 0 2018-01-02 20:43:55 编程技术 java java阅读题

引言

 随着工作时间的推移,参加过的面试也是挺多的。记录下参与面试中的一些经典代码阅读题。

 

对象与Map


Map取值测试:
package net.xqlee.project.demo;

public class Person {

	public Person(String name) {
		this.name = name;
	}

	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}




package net.xqlee.project.demo;

import java.util.HashMap;
import java.util.Map;

public class DemoMap {
	public static void main(String[] args) {

		Map<Person, String> map = new HashMap<>();
		map.put(new Person("leftso"), "leftso");
		map.put(new Person("小左"), "小左");

		String personName = map.get(new Person("leftso"));

		System.out.println(personName);

	}
}
控制台打印结果(提示:鼠标选中空白区域查看答案):
null
 

while循环

package net.xqlee.project.demo;

public class DemoWhile {

	public static void main(String[] args) {
		int y = 7;
		int x = 4;
		while ((x += 2) < y) {
			do {
				System.out.print(x + " ");
			} while (++y < 9);
		}
	}

}
控制台输出结果(提示:鼠标选中空白区域查看答案):
6 6 8 

整数Integer
 

package net.xqlee.project.demo;

public class DemoInteger {

	public static void main(String[] args) {
		Integer a = 20, b = 20, c = 150, d = 150;
		int a1 = 20, b1 = 20, c1 = 150, d1 = 150;
		System.out.print((a == b) + " " + (c == d) + " " + (a1 == b1) + " " + (c1 == d1));
	}

}

控制台输出:
true false true true

集合List

package net.xqlee.project.demo;

import java.util.ArrayList;
import java.util.List;

public class DemoList {
public static void main(String[] args) {
	List<Integer> list=new ArrayList<>();
	list.add(new Integer(20));
	list.add(new Integer(30));
	list.add(new Float(2.2));
	
	for (Integer integer : list) {
		System.out.print(integer+" ");
	}
}
}
控制台输出:
编译时错误




持续更新内容...