Object aaa="22";
System.out.println(aaa.toString());
结果:
22
String a = "dd";
System.out.println(a);
结果
dd
ArrayList<String> list = new ArrayList<>();
list.add("111");
list.add("222");
list.add("333");
System.out.println(list.toString());
结果
[111, 222, 333]
// 普通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());
}
HashSet<String> hashSet = new HashSet<>();
hashSet.add("111");
hashSet.add("222");
hashSet.add("333");
System.out.println(hashSet.toString());
结果
[111, 222, 333]
// 增强for
for (String s : hashSet) {
System.out.println(s);
}
int[] ints = {1, 2, 3, 4, 5, 6, 7};
System.out.println(ints.toString());
结果
[I@33c7353a
// 普通for循环
for (int i = 0; i < ints.length; i++) {
System.out.println(ints[i]);
}
// 增强for循环
for (int anInt : ints) {
System.out.println(anInt);
}
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
Set<String> strings = hashMap.keySet();
for (String key : strings) {
String value = hashMap.get(key);
System.out.println(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);
}