1. 程式人生 > >黑馬程式設計師--集合類總結1

黑馬程式設計師--集合類總結1

---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS開發</a>、<a href="http://edu.csdn.net"target="blank">.Net培訓</a>、期待與您交流! ----------------------

1.集合概述:

Java的集合類主要由兩個介面派生而出:CollectionMap,Collection和Map是Java集合框架的根介面,這兩個介面又包含了一些介面或實現類。Set和List介面是Collection介面派生的兩個子介面,Queue是Java提供的佇列實現,類似於List。Map實現類用於儲存具有對映關係的資料(key-value)。

Collection

|--List

|---ArrayList

|--LinkedList

|--Vector

|--Set

|--HashSet

|--TreeSet

|--Map

|--HaspMap

|--TreeMap

Collection常用方法:

注意:

集合必須只有物件, 集合中的元素不能是基本資料型別。

新增刪除:

boolean add(Object element)

 boolean remove(Object element)

查詢操作:

       int size()

        boolean isEmpty()

 boolean contains(Object element)

 Iterator iterator()

組操作:

 boolean containsAll(Collection collection)

 boolean addAll(Collection collection)

 void clear()

 void removeAll(Collection collection)

 void retainAll(Collection collection)

轉換操作:

 Object[] toArray()

Object[] toArray(Object[] a)

List:

(1)List的特點:
Collection
|--List
元素有序(儲存順序和取出順序一致),元素可以重複
|--Set
元素無序,唯一
(2)List的特有功能:
A:新增
add(int index,Object obj)
B:刪除
remove(int index)
C:獲取
Object get(int index)
D:列表迭代器
ListIterator listIterator()
E:修改
set(int index,Object obj)
(3)List的案例:
List儲存字串並遍歷:
List list = new ArrayList();


list.add("hello");
list.add("world");
list.add("java");


//迭代器
Iterator it = list.iterator();
while(it.hasNext())
{
String s = (String) it.next();
System.out.println(s);
}


//普通for迴圈
for(int x=0; x<list.size(); x++)
{
String s = (String)list.get(x);
System.out.println(s);
}


List儲存自定義物件並遍歷:


(4)ListIterator的特殊用法:
A:可以逆向遍歷,但是需要先正向遍歷,一般不用。


B:併發修改異常
原因:在迭代器遍歷的時候,不要使用集合修改集合本身。
解決方案:
A:使用迭代器遍歷的時候,通過迭代器修改集合。
B:使用集合遍歷,通過集合修改集合。


List集合的子類
(1)List的子類特點:
List
|--ArrayList
底層資料結構是陣列,查詢快,增刪慢。
執行緒不安全,效率高。
|--Vector
底層資料結構是陣列,查詢快,增刪慢。
執行緒安全,效率低。
|--LinkedList
底層資料結構是連結串列,查詢慢,增刪快。
執行緒不安全,效率高。
(2)到底使用誰?
需要安全嗎:
要:Vector
不要:ArrayList,LinkedList
查詢多:ArrayList
增刪多:LinkedList
(3)ArrayList
1:儲存字串並遍歷:
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java);
//方式1
Iterator<String> it = array.iterator();
while(it.hasNext())
{
String s = it.next();
System.out.println(s);
}
//方式2
for(int x=0; x<array.size(); x++)
{
String s = array.get(x);
System.out.println(s);
}
//方式3
for(String s : array)
{
System.out.println(s);

}

2:儲存自定義物件並遍歷:(自己補齊)
import java.util.ArrayList;
import java.util.Iterator;
public class LinkedList {
public static void main(String[] args) {
ArrayList<Student> array=new ArrayList<Student>();


Student s1=new Student("王獻之",23,"男");
Student s2=new Student("王之",23,"女");
Student s3=new Student("獻之",23,"女");
Student s4=new Student("王獻是",23,"男");
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
 Iterator<Student> it=array.iterator();
 while(it.hasNext())
 {
 Student s=it.next();
 System.out.println(s.getName()+".."+s.getAge()+"..."+s.getSex());
 }
}
}

4)LinkedList
儲存字串並遍歷:

import java.util.LinkedList;
import java.util.Iterator;
public class ArrayList {
public static void main(String[] args) {
LinkedList<String> list=new LinkedList<String>();
list.add("你");
list.add("好");
list.add("啊");
 Iterator<String> it=list.iterator();
 while(it.hasNext())
 {
 String s =it.next();
 System.out.println(s);
 }
}
}

儲存自定義物件並遍歷:

import java.util.LinkedList;
import java.util.Iterator;
public class LinkedList {
public static void main(String[] args) {
LinkedList<Student> list=new LinkedList<Student>();
Student s1=new Student("張之",23,"男");
Student s2=new Student("王之",23,"女");
Student s3=new Student("黎之",23,"女");
Student s4=new Student("王獻",23,"男");
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
 Iterator<Student> it=list.iterator();
 while(it.hasNext())
 {
 Student s=it.next();
 System.out.println(s.getName()+".."+s.getAge()+"..."+s.getSex());
 }
}
}


---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS開發</a>、<a href="http://edu.csdn.net"target="blank">.Net培訓</a>、期待與您交流! ----------------------


詳細請檢視:<a href="http://edu.csdn.net" target="blank">http://edu.csdn.net</a>