今天上午初識了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(返回一個迭代器,用于遍歷數組)
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(數組表,其中可以放任意對象)底層實現是數組(查詢操作快,增刪改操作慢、線程不安全效率低)
* LinkedList(鏈表)底層實現是鏈表(鏈表查詢慢,增刪改操作快、線程不安全效率低)
* Vector底層實現是數組,線程安全,效率低
* List list = new ArrayList();這是典型寫法,左邊是接口右邊為實現類(父類引用指向子類對象即多態)
*/
List list = new List();
System.out.println(list.getClass().getName());
ArrayList list2 = new ArrayList();
//list接口中定義了一個Object類的數組,其中可以放任意類型的對象
list2.add("123");//添加字符串對象String
list2.add(123);//此處編譯器自動將123包裝為integer類
list2.add(new Date());//添加時間對象Date
list2.add(new Dog());//添加自定義對象
System.out.println(list2.size());//打印數組中含有元素的長度(不是數組的長度,ArrayList中定義的數組長度可變的)
list2.remove("123");//移除數組
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());
//根順序有關的操作
list2.get(0);//返回數組索引為0的對象
list2.set(0, "456");//將數組索引為0的對象替換成String對象"456"
list2.remove(0);//將數組索引為0的對象移除掉"
//List list4 = new ArrayList();//接口List引用來指向ArrayList對象
}
}
class Dog{
}
階段知識總結:
【collection接口的概念】
1.collection表示一組對象,其作用是收集數據
2.Collection函數庫即java.util包下的一些類與接口。類用來產生對象,而接口是訪問數據的方式
3.Collection函數庫與數組的不同:
.數組容量是有限的,Collection庫的容量可以調節
.Collection函數庫只能用來存放對象,數組沒有限制
Collection接口是Collection層次結構中的根接口,它定義了一些最基本的訪問方法,讓我們能用統一的方式通過它和它的子類訪問數據
【Collection接口下的子類接口List、Set(子類HashSet)與Map(子類HashMap)的區別】
List中存放的數據有序可重復 Set中存放的數據無序不可重復,后面傳入的相同數據會覆蓋掉前面的數據
Map(鍵值對):通過一個對象找到另一個對象
【接口可以實現設計和實現的分離】
【Object與String類中equals方法區別】
Object中equals方法比較的是2個對象的地址(是否為同一對象) String重寫了Object的equals比較的是2個字符串變量的內容
【Array與LinkedList】
數組查詢操作快,增刪改操作慢;鏈表查詢慢,增刪改操作快