纸上得来终觉浅
1.首先List的实现类有下面几个:
1)ArrayList:底层采用数据结构中的数组,长度延长时延长100%;线程不同步
2)LinkedList:底层采用数据结构中的链表;线程不同步
3)Vector:底层采用数据结构中的数组,长度延长时自定义延长的百分比;线程同步
2.ArrayList:
<pre name="code" class="java">package roadArchitectWeb.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.ListIterator; class animal{ Integer age; @Override public String toString() { return "animal [age=" + age + "]"; } } public class Test2 { public static void main(String[] args) { ArrayList<Integer> arrayList = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9,10)); ArrayList<Integer> arrayList2 = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5)); /*addAll 添加一个集合*/ System.out.println("arrayList.addAll(arrayList2);:"+arrayList.addAll(arrayList2)); /*toArray 转化为数组*/ Object[] a = arrayList.toArray(); System.out.println("toArray:"+Arrays.toString(a)); /*iterator 获得迭代器 * next 获得下一个元素 * hasNext检查序列中是否还有元素 * remove 删除元素 * */ Iterator<Integer> iterator = arrayList.iterator(); System.out.println("iterator"); while(iterator.hasNext()){ System.out.print(iterator.next()); } iterator.remove(); System.out.println("删除最后一个元素过后:"+arrayList); /*add*/ arrayList.add(5); System.out.println("add过后:"+arrayList); /*add和addall 从指定位置插入*/ arrayList.add(arrayList.size(),100); arrayList.addAll(1,arrayList2); System.out.println("add 和 add all过后:"+arrayList); /*remove*/ arrayList.remove(0); arrayList.remove(10); System.out.println("remove过后:"+arrayList); /*subList获取子集合*/ List<Integer> list = arrayList.subList(0, 5); System.out.println("subList过后:"+list); /*Iterator只能查和删 * ListIterator可以增加和修改 * */ ListIterator<Integer> listIterator = arrayList.listIterator(); if(listIterator.hasNext()){ listIterator.next(); listIterator.add(333); } listIterator.next(); listIterator.set(90); System.out.println("ListIterator过后:"+arrayList); /*更改数据*/ ArrayList<animal> arrayList3 = new ArrayList<animal>(); arrayList3.add(new animal()); animal myAnimal = arrayList3.get(0); myAnimal.age = 100; System.out.println("get后修改值得到的结果是:"+arrayList3); } }结果如下:
<pre name="code" class="html">arrayList.addAll(arrayList2);:true toArray:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5] iterator 1234567891012345删除最后一个元素过后:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4] add过后:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5] add 和 add all过后:[1, 1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 100] remove过后:[1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 8, 9, 10, 1, 2, 3, 4, 5, 100] subList过后:[1, 2, 3, 4, 5] ListIterator过后:[1, 333, 90, 3, 4, 5, 2, 3, 4, 5, 6, 8, 9, 10, 1, 2, 3, 4, 5, 100] get后修改值得到的结果是:[animal [age=100]]其它的一些简单方法如indexOf就不讲了
3.之所以说ArrayList是线程不同步,是指多个线程同时作用于ArrayList的时候,ArrayList的操作没有获得同步,即同一时刻多个线程执行ArrayList的某个操作(函数);示例如下(以add方法为例):
package roadArchitectWeb.Test; import java.util.ArrayList; import java.util.Collections; import java.util.List; class Array implements Runnable{ /*使用Vector,线程安全*/ // protected static Vector<String> arrayList = new Vector<String>(); /*不加同步锁*/ // protected static List<String> arrayList = new ArrayList<String>(); /*加上同步锁*/ protected static List<String> arrayList = Collections.synchronizedList(new ArrayList<String>()); public void run(){ arrayList.add(Thread.currentThread().getName()); } } public class Test3 { public static void main(String[] args) throws InterruptedException { Array array = new Array(); for(int i=0;i<100;i++){ Thread thread = new Thread(array,String.valueOf(i)); thread.start(); } Thread.sleep(1000); System.out.println("Test3.main():"+array.arrayList); System.out.println("Test3.main():"+array.arrayList.size()); } }加上同步锁之后(或者使用Vector),结果如下:
Test3.main():[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 26, 28, 29, 30, 32, 31, 33, 34, 35, 37, 36,
38, 40, 39, 41, 42, 43, 45, 44, 46, 47, 48, 49, 51, 50, 52, 53, 54, 55, 56, 57, 58, 60, 61, 64, 63, 62, 59, 65, 68, 69, 70, 66, 67, 75, 73, 72, 74, 71, 76,
77, 78, 79, 80, 82, 83, 84, 81, 85, 86, 88, 89, 90, 91, 87, 92, 93, 94, 95, 96, 97, 98, 99] Test3.main():100不加同步锁,结果情况有多种,这里要展示的错误是:
Exception in thread "14" java.lang.ArrayIndexOutOfBoundsException: 15 at java.util.ArrayList.add(ArrayList.java:459) at roadArchitectWeb.Test.Array.run(Test3.java:14) at java.lang.Thread.run(Thread.java:745)出错的代码是下面的第三行:
public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; }在e赋值给elementData之后,size应该自增,但是多线程同时执行这句代码,可能实际上容量已经不够了,然后才自增,才让ensureCapacityInternal做扩充操作。就会出现ArrayIndexOutOfBoundsException的错误。
4.LinkedList
与ArrayList方法基本相同,增加了removeFirst,removeLast,addFirst,addLast操作等;同时它也是线程不安全的,也要加上同步锁;
5.Vector
方法什么的就不写了,就那些用法; 比较下与ArrayList的区别:
1)前者线程安全,ArrayList不是;
2)前者支持自定义扩充百分比,ArrayList百分百
3)前者速度稍低,ArrayList稍高;
注: 鼓励使用ArrayList