《幻塔》平民向武器意志與擬態搭配指南
阿新 • • 發佈:2022-01-03
程式碼1:
package com.atguigu.day15; import org.junit.Test; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.Iterator; /* 集合:儲存多個數據的,長度不固定 陣列:陣列的長度是固定的 Collection List:有序,按照新增的順序,不唯一,可以新增重複的元素 ArraysList LinkedList Set:無序,不是按照新增的順序輸出,唯一,不可以新增重複元素 HashSet LinkedHashSet TreeSet **/ public class Demo10 { @Test public void test1(){ //多型 Collection col1 = new ArrayList(); //新增元素 col1.add(10);//內部Integer.valueof(10) col1.add(20); col1.add(new Date()); System.out.println(col1);//[10, 20, Fri Jan 07 11:02:11 CST 2022] Collection col2= new ArrayList(); col2.add("張無忌"); col2.add("趙敏"); //add()會將一個集合作為一個元素處理 col1.add(col2); System.out.println(col1); //[10, 20, Fri Jan 07 11:08:01 CST 2022, [張無忌, 趙敏]] //addAll()會將集合中的每一個元素進行單獨處理 col1.addAll(col2); System.out.println(col1);//[10, 20, Fri Jan 07 11:08:01 CST 2022, [張無忌, 趙敏], 張無忌, 趙敏] System.out.println(col1.size());//長度為6 //判斷元素是否在集合內 boolean isExsit = col2.contains("張無忌"); System.out.println(isExsit);//true //清除指定元素 col1.remove(10); System.out.println(col1);//[20, Fri Jan 07 11:15:26 CST 2022, [張無忌, 趙敏], 張無忌, 趙敏] //刪除集合 col1.removeAll(col2); System.out.println(col1);//[20, Fri Jan 07 11:15:26 CST 2022, [張無忌, 趙敏]] Collection col3 = new ArrayList(); col3.add(10); col3.add(20); col3.add(30); Collection col4 = new ArrayList(); col4.add(10); col4.add(20); col3.retainAll(col4); System.out.println(col3);//[10, 20] //判斷集合是否為空 System.out.println("col3.isEmpty() = " + col3.isEmpty());//col3.isEmpty() = false //清除集合所有元素 col3.clear(); System.out.println(col3);//[] } @Test public void test2(){ Collection col1 = new ArrayList(); col1.add(10); col1.add(20); col1.add(30); col1.add("張無忌"); //遍歷集合第一種方法 for (Object ojb:col1){ System.out.println(ojb); } //遍歷集合第二種方法,通過建立迭代器實現 //hasNext()判斷遊標後面是否還有資料 //next()拿到指定的資料 Iterator iterator = col1.iterator(); while (iterator.hasNext()){ Object obj=iterator.next(); System.out.println(obj); } //轉換型別 Collection col2 = new ArrayList(); col2.add("abc"); col2.add("efg"); col2.add("bpf"); for (Object obj:col2){ String s = (String)obj; System.out.println(s); } System.out.println("-------------------"); //泛型:規定你輸入資料的型別 Collection<引用資料型別> Collection<String> col3 = new ArrayList<>(); col3.add("司馬亮"); col3.add("司馬瑋"); col3.add("司馬倫"); col3.add("司馬囧"); for (String s:col3){ if (s.equals("司馬倫")){ // col3.add("司馬乂");//此時會報錯,java.util.ConcurrentModificationException } } System.out.println(col3); System.out.println("-------------------"); Iterator<String> iterator1 = col3.iterator(); while (iterator1.hasNext()){ String s = iterator1.next(); if (s.equals("司馬倫")){ // col3.add("司馬乂");//此時會報錯,java.util.ConcurrentModificationException iterator1.remove(); } } System.out.println(col3); /*modcount:記錄集合的操作次數 在進行集合遍歷的時候,不要使用集合物件直接新增或者刪除元素,應該使用iterator去操作 */ } }
程式碼2:
package com.atguigu.day15; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class Demo11 { public static void main(String[] args) { Collection<StudentTest> colTest = new ArrayList<>(); StudentTest stu1 = new StudentTest("陸小鳳",98); StudentTest stu2 = new StudentTest("西門吹雪",38); StudentTest stu3 = new StudentTest("摘星子",58); colTest.add(stu1); colTest.add(stu2); colTest.add(stu3); Iterator<StudentTest> iterator = colTest.iterator(); while (iterator.hasNext()){ StudentTest stu = iterator.next(); if (stu.score<60){ iterator.remove(); } } System.out.println(colTest); } } class StudentTest{ String name; int score; public StudentTest() { } public StudentTest(String name, int score) { this.name = name; this.score = score; } @Override public String toString() { return "StudentTest{" + "name='" + name + '\'' + ", score=" + score + '}'; } }
輸出:
[StudentTest{name='陸小鳳', score=98}]
程式碼3: