今天上午初識了Collection(容器)差购,代碼如下:
package collection;
import java.util.Collection;
import java.util.Iterator;
public class TestCollection {
public static <E> void main(String[] args) {
//Collection有2個子類 List和Set
Collection collection = new Collection<E>() {
//@return the number of elements in this collection
public int size() {
return 0;
}
//@return <tt>true</tt> if this collection contains no element
public boolean isEmpty() {
return false;
}
// Returns <tt>true</tt> if this collection contains the specified element.
public boolean contains(Object o) {
return false;
}
//@return an <tt>Iterator</tt>over the elements in this collection(返回一個迭代器,用于遍歷數(shù)組)
public Iterator<E> iterator() {
return null;
}
//@return an array containing all of the elements in this collection
public Object[] toArray() {
return null;
}
public <T> T[] toArray(T[] a) {
return null;
}
public boolean add(E e) {
return false;
}
//Removes a single instance of the specified element from this
// collection, if it is present (optional operation).
public boolean remove(Object o) {
return false;
}
public boolean containsAll(Collection<?> c) {
return false;
}
public boolean addAll(Collection<? extends E> c) {
return false;
}
public boolean removeAll(Collection<?> c) {
return false;
}
public boolean retainAll(Collection<?> c) {
return false;
}
public void clear() {
}
};
}
}
package collection;
import java.util.ArrayList;
import java.util.Date;
public class List {
public static void main(String[] args) {
/**
* List 常用子類 ArrayList(數(shù)組表店茶,其中可以放任意對象)底層實現(xiàn)是數(shù)組(查詢操作快,增刪改操作慢屹篓、線程不安全效率低)
* LinkedList(鏈表)底層實現(xiàn)是鏈表(鏈表查詢慢技肩,增刪改操作快、線程不安全效率低)
* Vector底層實現(xiàn)是數(shù)組酒甸,線程安全,效率低
* List list = new ArrayList();這是典型寫法魄健,左邊是接口右邊為實現(xiàn)類(父類引用指向子類對象即多態(tài))
*/
List list = new List();
System.out.println(list.getClass().getName());
ArrayList list2 = new ArrayList();
//list接口中定義了一個Object類的數(shù)組,其中可以放任意類型的對象
list2.add("123");//添加字符串對象String
list2.add(123);//此處編譯器自動將123包裝為integer類
list2.add(new Date());//添加時間對象Date
list2.add(new Dog());//添加自定義對象
System.out.println(list2.size());//打印數(shù)組中含有元素的長度(不是數(shù)組的長度插勤,ArrayList中定義的數(shù)組長度可變的)
list2.remove("123");//移除數(shù)組
System.out.println(list2.size());
System.err.println(list2.isEmpty());
ArrayList list3 = new ArrayList();
list3.add("abc");
list2.addAll(list3);//將list3中的所有對象放到list2
System.out.println(list2.size());
//根順序有關(guān)的操作
list2.get(0);//返回數(shù)組索引為0的對象
list2.set(0, "456");//將數(shù)組索引為0的對象替換成String對象"456"
list2.remove(0);//將數(shù)組索引為0的對象移除掉"
//List list4 = new ArrayList();//接口List引用來指向ArrayList對象
}
}
class Dog{
}
階段知識總結(jié):
【collection接口的概念】
1.collection表示一組對象沽瘦,其作用是收集數(shù)據(jù)
2.Collection函數(shù)庫即java.util包下的一些類與接口革骨。類用來產(chǎn)生對象,而接口是訪問數(shù)據(jù)的方式
3.Collection函數(shù)庫與數(shù)組的不同:
.數(shù)組容量是有限的析恋,Collection庫的容量可以調(diào)節(jié)
.Collection函數(shù)庫只能用來存放對象良哲,數(shù)組沒有限制
Collection接口是Collection層次結(jié)構(gòu)中的根接口,它定義了一些最基本的訪問方法助隧,讓我們能用統(tǒng)一的方式通過它和它的子類訪問數(shù)據(jù)
【Collection接口下的子類接口List臂外、Set(子類HashSet)與Map(子類HashMap)的區(qū)別】
List中存放的數(shù)據(jù)有序可重復(fù) Set中存放的數(shù)據(jù)無序不可重復(fù),后面?zhèn)魅氲南嗤瑪?shù)據(jù)會覆蓋掉前面的數(shù)據(jù)
Map(鍵值對):通過一個對象找到另一個對象
【接口可以實現(xiàn)設(shè)計和實現(xiàn)的分離】
【Object與String類中equals方法區(qū)別】
Object中equals方法比較的是2個對象的地址(是否為同一對象) String重寫了Object的equals比較的是2個字符串變量的內(nèi)容
【Array與LinkedList】
數(shù)組查詢操作快喇颁,增刪改操作慢漏健;鏈表查詢慢,增刪改操作快