- 簡單示例
package com.mcq; import java.util.ArrayList; import java.util.Collection; public class CollectionDemo { public static void main(String[] args) { Collection collection=new ArrayList(); collection.add(100); collection.add("hello world"); collection.add(10.10); collection.add(true); collection.add(new Student("小明",19)); Object[] objArr=collection.toArray(); for(int i=0;i<objArr.length;i++){ if(i==objArr.length-1){ Student s=(Student)objArr[i]; System.out.println(s.getName()+" "+s.getAge()); } System.out.println(objArr[i]); } } } /* 100 hello world 10.1 true 小明 19 com.mcq.Student@7d67d940 */
package com.mcq; import java.util.Collection; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import com.sun.org.apache.bcel.internal.generic.NEW; public class CollectionDemo2 { public static void main(String[] args) { Collection c = new ArrayList(); c.add(new Student("小明", 19)); c.add(new Student("小麗", 18)); System.out.println(c.size()); Iterator iterator = c.iterator(); Student student = (Student) iterator.next();// iterator初始相當於頭結點,無值。 System.out.println(student.getName() + " " + student.getAge()); while (iterator.hasNext()) { Object next = iterator.next(); Student s = (Student) next; System.out.println(s.getName() + " " + s.getAge()); } System.out.println(); List list = (List) c;// List繼承自Collection for (int i = 0; i < c.size(); i++) { Object object = list.get(i); Student s = (Student) object; System.out.println(s.getName() + " " + s.getAge()); } System.out.println(); Iterator iterator2 = list.iterator(); while (iterator2.hasNext()) { Object next = iterator2.next(); Student student2 = (Student) next; System.out.println(student2.getName() + " " + student2.getAge()); } System.out.println(); for (int i = 0; i < list.size(); i++) { Student s=(Student)list.get(i); if(s.getName().equals("小明")){ s.setName("小豬"); } } iterator = list.iterator(); while (iterator.hasNext()) { Object next = iterator.next(); Student s = (Student) next; System.out.println(s.getName() + " " + s.getAge()); } System.out.println(); list.set(0, new Student("小花", 20));//用指定的對象替換下表對應的對象。 iterator = list.iterator(); while (iterator.hasNext()) { Object next = iterator.next(); Student s = (Student) next; System.out.println(s.getName() + " " + s.getAge()); } } } /* 2 小明 19 小麗 18 小明 19 小麗 18 小明 19 小麗 18 小豬 19 小麗 18 小花 20 小麗 18 */
Collection和List都是接口,ArrayList類實現這些接口,集合遍歷的時候,使用iterator迭代器,用add()方法添加元素會出現併發修改異常。
-
ArrayList用法類似。
-
LinkedList用法:
package com.mcq; import java.util.Iterator; import java.util.LinkedList; import com.sun.org.apache.bcel.internal.generic.NEW; public class LinkedListDemo { public static void main(String[] args) { LinkedList list=new LinkedList(); list.add("小花"); list.add("小明"); Iterator iterator=list.iterator(); while(iterator.hasNext()){ String s=(String)iterator.next(); System.out.println(s); } System.out.println("--------------"); list.addFirst("小強");//添加到表頭 iterator=list.iterator(); while(iterator.hasNext()){ String s=(String)iterator.next(); System.out.println(s); } System.out.println("---------------"); System.out.println(list.getFirst());//獲得首元素 System.out.println("---------------"); System.out.println(list.pop());//刪除首元素 System.out.println("---------------"); iterator=list.iterator(); while(iterator.hasNext()){ String s=(String)iterator.next(); System.out.println(s); } } } /* 小花 小明 -------------- 小強 小花 小明 --------------- 小強 --------------- 小強 --------------- 小花 小明 */
線程不安全(線程不同步):如果出現併發需求,需要你去手動進行線程安全操作。也就是說如果多個用戶同時訪問一個對象的時候,一邊修改了另一邊可能就會出現錯誤。但效率高。
-
ArrayList和LinkedList相同點:(1)都是線程不安全的。(2)實現了接口。
-
不同點:(1)前者是數組實現,後者是鏈表實現。(2)前者適合查詢;後者適合修改。
java學習筆記之Collection、List、ArrayList、LinkedList
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.