遍历数组
String[] arr = { "包青天", "白乾涛", "baiqiantao", "bqt", "0909082401", }; for (int i = 0; i < arr.length; i++) {//普通for System.out.println("第" + (i + 1) + "个元素为:" + arr[i]); } for (String i : arr) {//增强for System.out.println("元素为:" + i); }
遍历List、Set
List<String> mList = new ArrayList<String>(); mList.add("包青天a"); mList.add("包青天c"); mList.add("包青天b"); System.out.println(mList); //[包青天a, 包青天c, 包青天b] for (String string : mList) {//增强for System.out.println("---" + string); } Iterator<String> it = mList.iterator(); while (it.hasNext()) {//迭代器 System.out.println(it.next()); }
//list特有的ListIterator,可以双向遍历 ListIterator<String> it2 = mList.listIterator(); while (it2.hasNext()) {//List迭代器 System.out.println("*******" + it2.next());//执行完后指针移到了最后,这有这样才能使用hasPrevious遍历 } while (it2.hasPrevious()) {//List迭代器 System.out.println(it2.previous() + "*****"); }
遍历Map
Map<Integer, String> map = new HashMap<Integer, String>(); map.put(30, "zhagsan"); map.put(17, "李四"); map.put(2016, "包青天"); System.out.println(map);//{17=李四, 2016=包青天, 30=zhagsan} for (Integer key : map.keySet()) {//增强for遍历key String value = map.get(key); System.out.println(key + "---" + value); } for (Map.Entry<Integer, String> k_v : map.entrySet()) {//增强for遍历键值对 System.out.println(k_v);//17=李四 }
Iterator<Integer> iterator = map.keySet().iterator(); while (iterator.hasNext()) {//健的Set集合的迭代器 Integer key = iterator.next(); String value = map.get(key); System.out.println(key + ":" + value); } Iterator<Map.Entry<Integer, String>> iterator2 = map.entrySet().iterator(); while (iterator2.hasNext()) {//键值对的迭代器 System.out.println(iterator2.next());//17=李四 }
List嵌套Map
ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String, String>>(); HashMap<String, String> hm1 = new HashMap<String, String>(); hm1.put("周瑜", "小乔"); hm1.put("吕布", "貂蝉"); // 把元素添加到array里面 array.add(hm1); HashMap<String, String> hm2 = new HashMap<String, String>(); hm2.put("郭靖", "黄蓉"); hm2.put("杨过", "小龙女"); // 把元素添加到array里面 array.add(hm2);
// 遍历 for (HashMap<String, String> hm : array) { Set<String> set = hm.keySet(); for (String key : set) { String value = hm.get(key); System.out.println(key + "-" + value); } System.out.println("//**************************"); } }
Map嵌套Map
HashMap<String, HashMap<String, Integer>> mMap = new HashMap<String, HashMap<String, Integer>>(); HashMap<String, Integer> jcMap = new HashMap<String, Integer>(); jcMap.put("陈玉楼", 20); jcMap.put("高跃", 22); mMap.put("基础班", jcMap); HashMap<String, Integer> jyMap = new HashMap<String, Integer>(); jyMap.put("李杰", 21); jyMap.put("曹石磊", 23); mMap.put("就业班", jyMap); //遍历集合 Set<String> mKeySet = mMap.keySet(); for (String mKey : mKeySet) { System.out.println(mKey); HashMap<String, Integer> mValue = mMap.get(mKey); Set<String> mValueKeySet = mValue.keySet(); for (String mValueKey : mValueKeySet) { Integer mValueValue = mValue.get(mValueKey); System.out.println("\t" + mValueKey + "-" + mValueValue); } }
Map嵌套List
HashMap<String, ArrayList<String>> hashMap = new HashMap<String, ArrayList<String>>(); ArrayList<String> array1 = new ArrayList<String>(); array1.add("吕布"); array1.add("周瑜"); array1.add("包青天"); hashMap.put("三国演义人物", array1); ArrayList<String> array2 = new ArrayList<String>(); array2.add("令狐冲"); array2.add("林平之"); array2.add("包青天"); hashMap.put("笑傲江湖人物", array2); //遍历集合 Set<String> keySet = hashMap.keySet(); for (String key : keySet) { System.out.println(key); ArrayList<String> valueList = hashMap.get(key); for (String string : valueList) { System.out.println("\t" + string); } }
多层嵌套
HashMap<String, HashMap<String, ArrayList<Student>>> mMap = new HashMap<String, HashMap<String, ArrayList<Student>>>(); // 北京校区数据 HashMap<String, ArrayList<Student>> bjmMap = new HashMap<String, ArrayList<Student>>(); ArrayList<Student> array1 = new ArrayList<Student>(); Student s1 = new Student("林青霞", 27); Student s2 = new Student("风清扬", 30); array1.add(s1); array1.add(s2); ArrayList<Student> array2 = new ArrayList<Student>(); Student s3 = new Student("赵雅芝", 28); Student s4 = new Student("武鑫", 29); array2.add(s3); array2.add(s4); bjmMap.put("基础班", array1); bjmMap.put("就业班", array2); mMap.put("北京校区", bjmMap); // 广州校区数据 HashMap<String, ArrayList<Student>> xamMap = new HashMap<String, ArrayList<Student>>(); ArrayList<Student> array3 = new ArrayList<Student>(); Student s5 = new Student("范冰冰", 27); Student s6 = new Student("刘意", 30); array3.add(s5); array3.add(s6); ArrayList<Student> array4 = new ArrayList<Student>(); Student s7 = new Student("李冰冰", 28); Student s8 = new Student("张志豪", 29); array4.add(s7); array4.add(s8); xamMap.put("基础班", array3); xamMap.put("就业班", array4); mMap.put("广州校区", xamMap); // 遍历集合 Set<String> mMapSet = mMap.keySet(); for (String mMapKey : mMapSet) { System.out.println(mMapKey); HashMap<String, ArrayList<Student>> mMapValue = mMap.get(mMapKey); Set<String> mMapValueSet = mMapValue.keySet(); for (String mMapValueKey : mMapValueSet) { System.out.println("\t" + mMapValueKey); ArrayList<Student> mMapValueValue = mMapValue.get(mMapValueKey); for (Student s : mMapValueValue) { System.out.println("\t\t" + s.getName() + "-" + s.getAge()); } } }
时间: 2024-10-14 12:45:40