一、Object
1、tostring
Object aaa="22"; System.out.println(aaa.toString());
结果:
22
二、String
1、tostring
String a = "dd"; System.out.println(a);
结果
dd
三、List
1、tostring
ArrayList<String> list = new ArrayList<>(); list.add("111"); list.add("222"); list.add("333"); System.out.println(list.toString());
结果
[111, 222, 333]
2、遍历
// 普通for训话 for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } // 增加for for (String s : list) { System.out.println(s); } // 迭代器 Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }
四、Set
1、tostring
HashSet<String> hashSet = new HashSet<>(); hashSet.add("111"); hashSet.add("222"); hashSet.add("333"); System.out.println(hashSet.toString());
结果
[111, 222, 333]
2、遍历
// 增强for for (String s : hashSet) { System.out.println(s); }
五、int[ ]
1、tostring
int[] ints = {1, 2, 3, 4, 5, 6, 7}; System.out.println(ints.toString());
结果
[I@33c7353a
2、遍历
// 普通for循环 for (int i = 0; i < ints.length; i++) { System.out.println(ints[i]); } // 增强for循环 for (int anInt : ints) { System.out.println(anInt); }
六、Map
1、tostring
HashMap<String, String> hashMap = new HashMap<>(); hashMap.put("key01", "value01"); hashMap.put("key02", "value02"); hashMap.put("key03", "value03"); hashMap.put("key04", "value04"); System.out.println(hashMap.toString());
结果
ToString
2、遍历
- 通过key获取value值
Set<String> strings = hashMap.keySet(); for (String key : strings) { String value = hashMap.get(key); System.out.println(key + "==" + value); }
- 获取每个的key+value的集合
Set<Map.Entry<String, String>> entries = hashMap.entrySet(); for (Map.Entry<String, String> entry : entries) { String key = entry.getKey(); String value = entry.getValue(); System.out.println(key + "==" + value); }
- 通过迭代器
Set<Map.Entry<String, String>> entries1 = hashMap.entrySet(); Iterator<Map.Entry<String, String>> iterator2 = entries1.iterator(); if (iterator2.hasNext()) { Map.Entry<String, String> next = iterator2.next(); String key = next.getKey(); String value = next.getValue(); System.out.println(key + "==" + value); }
七、Xmind整理