只要是会点代码的,面试总逃不开map、arraylist这些,直接贴代码吧
JsonObject
JSONObject json = new JSONObject(); json.put("username","alice"); json.put("password","1234"); json.put("age",15); System.out.println(json); Setkeyset= json.keySet(); Iterator iterator = keyset.iterator(); while(iterator.hasNext()){ String key = iterator.next(); String value = json.getString(key); System.out.println(key+":"+value); }
运行结果:
{"password":"1234","age":15,"username":"alice"} password:1234 age:15 username:alice
Set
Setset = new HashSet (); set.add("aaa"); set.add("bbb"); set.add("ccc"); System.out.println(set); set.remove("aaa"); for(String str:set){ System.out.println(str); }
运行结果:
[aaa, ccc, bbb]
ccc
bbb
Map
Mapmap = new HashMap (); map.put("username","alice"); map.put("password","1234"); map.put("age",15); System.out.println(map); Set keySet= json.keySet(); for(String mapkey:keySet){ String mapvalue = json.getString(mapkey); System.out.println(mapkey+":"+mapvalue); }
运行结果:
{password=1234, age=15, username=alice}
password:1234
username:alice
ArrayList
public static void main(String[] args) { ArrayListarrayList = new ArrayList (); arrayList.add("a"); arrayList.add("b"); arrayList.add("c"); arrayList.add("d"); System.out.println(arrayList); arrayList.remove(1); System.out.println(arrayList); for(int i=0;i
打印结果:
[a, b, c, d]
[a, c, d]
a
c
d
List
Listlist = new ArrayList (); list.add("a"); list.add("b"); list.add("c"); list.add("d"); System.out.println(list); list.remove(1); System.out.println(list); for(int i=0;i 运行结果:
[a, b, c, d]
[a, c, d]
a
c
d