-
ArrayList(重点):
- 数组结构实现,查询快,增删慢
- jdk1.2版本,运行效率快,线程不安全
- 源码分析:
- 默认容量大小:DEFAULT_CAPACITY = 10(如果没有向集合中添加任何元素容量为0,添加一个元素容量为10,每次扩容大小为原来的1.5倍)
- 存放元素的数组:elementData
- 实际的元素个数:size
- 添加元素:add()
//创建集合 ArrayList arrayList = new ArrayList(); //添加元素 Student s1 = new Student("张三",20); Student s2 = new Student("李四",21); Student s3 = new Student("王五",22); ArrayList.add(s1); ArrayList.add(s2); ArrayList.add(s3); //遍历(重点) //迭代器 Iterator it = arrayList.iterator(); while(it.hasNext()){ Student s = (Student)it.next(); System.out.println(s.toString()); } //列表迭代器 ListIterator lit = arrayList.listIterator(); //正序 while(lit.hasNext()){ Student s = (Student)lit.next(); System.out.println(s.toString()); } //逆序 while(lit.hasPrevious()){ Student s = (Student)lit.previous(); System.out.println(s.toString()); } //判断 System.out.println(arrayList.contains(new Student("李四",21))); //true重写equals方法可判断 System.out.println(arrayList.isEmpty()); //false //查找 System.out.println(arrayList.indexOf(new Student("李四",21)); //1 //删除元素 arrayList.remove(s1); arrayList.remove(new Student("王五",22)); //重写equals方法后可删除
-
Vector:
- 数组结构实现,查询快,增删慢
- jdk1.0版本,运行效率慢,线程安全
//创建集合 Vector vector = new Vector(); //添加元素 vector.add("apple"); vector.add("orange"); vector.add("pear"); //遍历 //for 增强for 迭代器也可以 //枚举器 Enumeration en = vector.elements(); while(en.hasMoreElements()){ String o = (String)en.nextElement(); System.out.println(o); } //firstElement()、lastElement()、elementAt();方法 System.out.println(vector.firstElement()); //apple System.out.println(vector.lastElement()); //pear System.out.println(vector.elementAt(1)); //orange //判断 System.out.println(vector.contains("apple")); //true System.out.println(vector.isEmpty()); //false //删除元素 collection.remove("pear"); System.out.println("删除之后:" + vector.size()); //删除之后:2 collection.clear(); System.out.println("删除之后:" + vector.size()); //删除之后:0
-
LinkedList:
- 链表结构实现,增删快,查询慢(双向链表)
- 源码分析:
- 集合的大小:size
- 链表的头节点:Node first
- 链表的尾节点:Node last
//创建集合 LinkedList linkedList = new LinkedList(); //添加元素 Student s1 = new Student("张三",20); Student s2 = new Student("李四",21); Student s3 = new Student("王五",22); linkedList.add(s1); linkedList.add(s2); linkedList.add(s3); //遍历(重点) //for遍历 for(int i = 0; i < linkedList.size(); i++){ System.out.println(linkedList.get(i)); } //增强for for(Object o : linkedList){ Student s = (Student)o; System.out.println(s.toString()); } //迭代器 Iterator it = linkedList.iterator(); while(it.hasNext()){ Student s = (Student)it.next(); System.out.println(s.toString()); } //列表迭代器 ListIterator lit = linkedList.listIterator(); //正序 while(lit.hasNext()){ Student s = (Student)lit.next(); System.out.println(s.toString()); } //逆序 while(lit.hasPrevious()){ Student s = (Student)lit.previous(); System.out.println(s.toString()); } //判断 System.out.println(linkedList.contains(new Student("李四",21))); //true重写equals方法可判断 System.out.println(linkedList.isEmpty()); //false //查找 System.out.println(linkedList.indexOf(new Student("李四",21)); //1 System.out.println(linkedList.indexOf(s1); //0 //删除元素 linkedList.remove(s1); linkedList.remove(new Student("王五",22)); //重写equals方法后可删除
-
ArrayList和LinkedList的区别
- ArrayList:必须开辟连续空间,查询快,增删慢。LinkedList:无需开辟连续的空间,查询慢,增删快。
- ArrayList:必须开辟连续空间,查询快,增删慢。LinkedList:无需开辟连续的空间,查询慢,增删快。
List实现类
最新推荐文章于 2024-10-01 18:51:18 发布