List接口常用方法:
package com.CollectionTest;import com.codeday23.demo01.WindowTest;import org.junit.Test;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.List;public class ClollectionTest1 {/*** Collection接口中声明的方法的测试** 向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals().**/@Testpublic void test1(){Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("Tom"));coll.add(false);coll.add(new Person("Jerry",20));// 判断当前集合中是否包含objboolean contains = coll.contains(123);System.out.println(contains);System.out.println(coll.contains(new String("Tom")));System.out.println(coll.contains(new Person("Jerry",20)));// 没有重写equals方法是false//2、containsAll(Collection coll1):形参coll1中的所有元素是否都存在于当前集合中Collection coll1 = Arrays.asList(123,456);// 返回一个listSystem.out.println(coll.containsAll(coll1));}@Testpublic void test2(){// 3、remove(Object obj)Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("Tom"));coll.add(false);coll.add(new Person("Jerry",20));System.out.println(coll);coll.remove(123);System.out.println(coll);// 4、removeAll(Collection coll1):从当前集合中一处coll1中所有的元素Collection coll1 = Arrays.asList(123,456);coll.removeAll(coll1);System.out.println(coll);}@Testpublic void test3(){Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("Tom"));coll.add(false);coll.add(new Person("Jerry",20));// 求交集Collection coll1 = Arrays.asList(123,456,789);coll.retainAll(coll1);System.out.println(coll);}@Testpublic void test4(){Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("Tom"));coll.add(false);coll.add(new Person("Jerry",20));Collection coll1 = new ArrayList();coll1.add(123);coll1.add(456);coll1.add(new String("Tom"));coll1.add(false);coll1.add(new Person("Jerry",20));// equals(Object obj):对比两个集合是否一样,注意是list里面的对象是有序的,// 比较时候也会考虑这一点System.out.println(coll.equals(coll1));// 返回当前对象的hash值System.out.println(coll.hashCode());// 集合--->数组Object[] arr = coll.toArray();for (int i = 0; i < arr.length; i++) {System.out.println(arr[i]);}// 拓展:数组-->集合List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});System.out.println(list);List<int[]> arr1 = Arrays.asList(new int[]{123, 456});// 这样写的话会被认为是一个元素List<Integer> arr2 = Arrays.asList(new Integer[]{123, 456});// 包装类会被认为是两个元素System.out.println(arr1);System.out.println(arr2);// iterator():返回Iterator接口的实例,用于遍历集合元素。}}
迭代器:
package com.CollectionTest;import org.junit.Test;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;/*** 使用Iterator迭代器遍历集合中的元素*/public class IteratorTest {@Testpublic void test1(){Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("Tom"));coll.add(false);coll.add(new Person("Jerry",20));Iterator iterator = coll.iterator();// 遍历方式一// System.out.println(iterator.next());// System.out.println(iterator.next());// System.out.println(iterator.next());// System.out.println(iterator.next());// System.out.println(iterator.next());// 遍历方式二:// for(int i = 0;i < coll.size();i++){// System.out.println(iterator.next());// }// 遍历方式三(推荐):while(iterator.hasNext()){// 起始点是-1,下移变成0System.out.println(iterator.next());}}@Testpublic void test2(){Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("Tom"));coll.add(false);coll.add(new Person("Jerry",20));// 错误方式一// Iterator iterator = coll.iterator();// while ((iterator.next()) != null){// System.out.println(iterator.next());// }// 每次都要new一个匿名对象,所以会死循环并且一直是第一个while ((coll.iterator().hasNext())){System.out.println(coll.iterator().next());}}// 测试迭代器中的remove()方法@Testpublic void test3() {Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("Tom"));coll.add(false);coll.add(new Person("Jerry", 20));// 删除集合中"Tom"这个数组Iterator iterator = coll.iterator();while(iterator.hasNext()){Object obj = iterator.next();// 这里的object只是临时的对象,仅仅是拿来比较的if ("Tom".equals(obj)){iterator.remove();// remove()一定是迭代器获取当前的对象才可以调}}//遍历集合iterator = coll.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}}}
增强for循环:
package com.CollectionTest;import org.junit.Test;import java.util.ArrayList;import java.util.Collection;/*** JDK5.0新增了foreach循环,用于遍历集合、数组*/public class ForTest {@Testpublic void test1(){Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("Tom"));coll.add(false);coll.add(new Person("Jerry",20));// for (集合元素的类型 局部变量 :集合对象)// 内部还是迭代器for(Object obj : coll){System.out.println(obj);}}@Testpublic void test2(){int[] arr = new int[]{1,2,3,4,5,6};for(int i : arr){System.out.println(i);}}@Testpublic void test3(){String[] arr = new String[]{"MM","MM","MM"};// for (int i = 0; i < arr.length;i++) {// arr[i] = "GG";// }//// for (int i = 0; i < arr.length; i++) {// System.out.println("普通for循环:"+ arr[i]);// }for(String s : arr){s = "GG";// 这个for循环是先把对象取出来赋值给s,然后再改变s的值来实现的,// 没有赋值回去,而正常for循环是直接把字符串地址赋值给集合中的对象}for (int i = 0; i < arr.length; i++) {System.out.println("增强for循环:" + arr[i]);}}}
注意普通for循环和增强for循环运行的差异
/*
List接口中三个实现类ArrayList\LinkedLiset\Vector的异同:
ArrayList是主要实现类,线程不安全,效率高,底层使用Object[]存储
LinkedList对频繁的插入和删除操作,使用此类比Arraylist高,底层使用双向链表存储
Vector是古老的实现类,线程安全,效率低,底层使用Object[]
ArrayList的源码分析:
jdk7情况下
ArrayList list = new ArrayList();// 底层创建长度为10的Object[]数组
List.add(123);// elementData[0] = new Integer(123);
…
list.add(11);// 如果此次的添加导致底层elementData数组容量不够,则扩容
默认情况下扩容到原来长度的1.5倍,同时需要将原来数组中的数据赋值到新的数组中。
结论:建议开发中使用带参构造器:ArrayList = new ArrayList(int capacity)
jdk8情况下
ArrayList list = new ArrayList();// 底层Object[] elementData初始化为{},
并没有创建初始化的数组。
list.add(123);// 第一次调用add()时,底层才创建了长度10的数组,并将数据123添加到
elementData中
…后续添加和扩容的操作与jdk7无异。
小结:jdk7类似饿汉式,jdk8类似懒汉式
LinkList源码分析:
LinkedList list = new LinkedList();// 内部声明了first和last属性,默认值为null
list.add(123);// 将123封装到Node中,创建了Node对象。
其中Node定义为,体现了双向列表的说法:
private static class Node
E item;
Node
Node
Node(Node
this.next = next;
this.prev = prev;
}
}
Vector源码分析:两个版本都创建了长度为10的数组,扩容时扩容为两倍
同:都实现了List的接口,存储的特点相同:存储有序的、可重复的数据
不同:见上
/
List接口方法:_void add(int index, Object ele)_:在index位置插入ele元素 _boolean addAll(int index, Collection eles)_:从index位置开始将eles中 的所有元素添加进来 _Object get(int index)_:获取指定index位置的元素 _int indexOf(Object obj)_:返回obj在集合中首次出现的位置 _int lastIndexOf(Object obj)_:返回obj在当前集合中末次出现的位置 _Object remove(int index)_:移除指定index位置的元素,并返回此元素 _Object set(int index, Object ele)_:设置指定index位置的元素为ele _List subList(int fromIndex, int toIndex)_:返回从fromIndex到toIndex 位置的子集合
总结:
- 增:
add() - 删:
remove() - 改:
set() - 查:
get() - 插:
add(int index) - 长度:
size() - 遍历:
1)Iterator;
2)增强for循环;
3)普通的循环。
