1.1 List常用方法
A:添加功能
boolean add(E e):向集合中添加一個元素
void add(int index, E element):在指定位置添加元素
boolean addAll(Collection<? extends E> c):向集合中添加一個集合的元素。
B:刪除功能
void clear():刪除集合中的所有元素
E remove(int index):根據(jù)指定索引刪除元素,并把刪除的元素返回
boolean remove(Object o):從集合中刪除指定的元素
boolean removeAll(Collection<?> c):從集合中刪除一個指定的集合元素。
C:修改功能
E set(int index, E element):把指定索引位置的元素修改為指定的值,返回修改前的值。
D:獲取功能
E get(int index):獲取指定位置的元素
Iterator iterator():就是用來獲取集合中每一個元素。
E:判斷功能
boolean isEmpty():判斷集合是否為空。
boolean contains(Object o):判斷集合中是否存在指定的元素。
boolean containsAll(Collection<?> c):判斷集合中是否存在指定的一個集合中的元素。
F:長度功能
int size():獲取集合中的元素個數(shù)
G:把集合轉(zhuǎn)換成數(shù)組
Object[] toArray():把集合變成數(shù)組。
public class ArrayListTest {
? ? public static void main(String[] agrs){
? ? ? ? //創(chuàng)建ArrayList集合:
? ? ? ? List<String> list = new ArrayList<String>();
? ? ? ? System.out.println("ArrayList集合初始化容量:"+list.size());
? ? ? ? //添加功能:
? ? ? ? list.add("Hello");
? ? ? ? list.add("world");
? ? ? ? list.add(2,"!");
? ? ? ? System.out.println("ArrayList當前容量:"+list.size());
? ? ? ? //修改功能:
? ? ? ? list.set(0,"my");
? ? ? ? list.set(1,"name");
? ? ? ? System.out.println("ArrayList當前內(nèi)容:"+list.toString());
? ? ? ? //獲取功能:
? ? ? ? String element = list.get(0);
? ? ? ? System.out.println(element);
? ? ? ? //迭代器遍歷集合:(ArrayList實際的跌倒器是Itr對象)
? ? ? ? Iterator<String> iterator =? list.iterator();
? ? ? ? while(iterator.hasNext()){
? ? ? ? ? ? String next = iterator.next();
? ? ? ? ? ? System.out.println(next);
? ? ? ? }
? ? ? ? //for循環(huán)迭代集合:
? ? ? ? for(String str:list){
? ? ? ? ? ? System.out.println(str);
? ? ? ? }
? ? ? ? //判斷功能:
? ? ? ? boolean isEmpty = list.isEmpty();
? ? ? ? boolean isContain = list.contains("my");
? ? ? ? //長度功能:
? ? ? ? int size = list.size();
? ? ? ? //把集合轉(zhuǎn)換成數(shù)組:
? ? ? ? String[] strArray = list.toArray(new String[]{});
? ? ? ? //刪除功能:
? ? ? ? list.remove(0);
? ? ? ? list.remove("world");
? ? ? ? list.clear();
? ? ? ? System.out.println("ArrayList當前容量:"+list.size());
? ? }
}
1.3LinkedList基本操作
public class LinkedListTest {
? ? public static void main(String[] agrs){
? ? ? ? List<String> linkedList = new LinkedList<String>();
? ? ? ? System.out.println("LinkedList初始容量:"+linkedList.size());
? ? ? ? //添加功能:
? ? ? ? linkedList.add("my");
? ? ? ? linkedList.add("name");
? ? ? ? linkedList.add("is");
? ? ? ? linkedList.add("jiaboyan");
? ? ? ? System.out.println("LinkedList當前容量:"+ linkedList.size());
? ? ? ? //修改功能:
? ? ? ? linkedList.set(0,"hello");
? ? ? ? linkedList.set(1,"world");
? ? ? ? System.out.println("LinkedList當前內(nèi)容:"+ linkedList.toString());
? ? ? ? //獲取功能:
? ? ? ? String element = linkedList.get(0);
? ? ? ? System.out.println(element);
? ? ? ? //遍歷集合:(LinkedList實際的跌倒器是ListItr對象)
? ? ? ? Iterator<String> iterator =? linkedList.iterator();
? ? ? ? while(iterator.hasNext()){
? ? ? ? ? ? String next = iterator.next();
? ? ? ? ? ? System.out.println(next);
? ? ? ? }
? ? ? ? //for循環(huán)迭代集合:
? ? ? ? for(String str:linkedList){
? ? ? ? ? ? System.out.println(str);
? ? ? ? }
? ? ? ? //判斷功能:
? ? ? ? boolean isEmpty = linkedList.isEmpty();
? ? ? ? boolean isContains = linkedList.contains("jiaboyan");
? ? ? ? //長度功能:
? ? ? ? int size = linkedList.size();
? ? ? ? //刪除功能:
? ? ? ? linkedList.remove(0);
? ? ? ? linkedList.remove("jiaboyan");
? ? ? ? linkedList.clear();
? ? ? ? System.out.println("LinkedList當前容量:" + linkedList.size());
? ? }
}
? ? ? ? //刪除功能:
? ? ? ? linkedList.remove(0);
? ? ? ? linkedList.remove("jiaboyan");
? ? ? ? linkedList.clear();
? ? ? ? System.out.println("LinkedList當前容量:" + linkedList.size());
? ? }
}