List集合
(有序的collection)
List子接口
-
特点有序、有下标、元素可以循环。
-
方法:
- void add(int index,Object o) //在index位置插入对象o。
- boolean addAll(int index,Collection c) //将一个集合中的元素添加到此集合中的index位置。
- Object get(int index) //返回集合中指定位置的元素。
- List subList(int fromIndex,int toIndex) //返回fromIndex和toIndex之间的集合元素。
| package com.collectionsFramework.demo01; |
| |
| import java.util.ArrayList; |
| import java.util.Iterator; |
| import java.util.List; |
| import java.util.ListIterator; |
| |
| |
| |
| |
| |
| public class Demo03 { |
| public static void main(String[] args) { |
| |
| List list = new ArrayList<>(); |
| |
| list.add("苹果"); |
| list.add("小米"); |
| list.add(0,"华为"); |
| System.out.println("元素个数:"+list.size()); |
| System.out.println(list.toString()); |
| |
| |
| |
| |
| |
| |
| |
| System.out.println("-----------3.1使用for遍历----------"); |
| for (int i = 0; i < list.size(); i++) { |
| System.out.println(list.get(i)); |
| } |
| |
| System.out.println("-----------3.2使用增强for----------"); |
| for (Object object:list) { |
| System.out.println(object); |
| } |
| |
| Iterator it = list.iterator(); |
| System.out.println("-----------3.3使用迭代器----------"); |
| while (it.hasNext()){ |
| System.out.println(it.next()); |
| } |
| |
| ListIterator lit = list.listIterator(); |
| System.out.println("-----------3.4使用列表迭代器从前往后----------"); |
| while (lit.hasNext()){ |
| System.out.println(lit.nextIndex()+":"+lit.next()); |
| } |
| System.out.println("-----------3.4使用列表迭代器从后往前----------"); |
| while (lit.hasPrevious()){ |
| System.out.println(lit.previousIndex()+":"+lit.previous()); |
| } |
| |
| System.out.println(list.contains("苹果")); |
| System.out.println(list.isEmpty()); |
| |
| |
| System.out.println(list.indexOf("华为")); |
| } |
| } |
| package com.collectionsFramework.demo01; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| |
| |
| |
| public class Demo04 { |
| public static void main(String[] args) { |
| |
| List list = new ArrayList(); |
| |
| list.add(20); |
| list.add(30); |
| list.add(40); |
| list.add(50); |
| list.add(60); |
| System.out.println("元素个数:"+list.size()); |
| System.out.println(list.toString()); |
| |
| |
| |
| |
| |
| |
| |
| |
| List subList = list.subList(1,3); |
| System.out.println(subList.toString()); |
| |
| } |
| } |
List实现类
ArrayList【重点】
- 数组结构实现,查询快、增删慢;
- JDK1.2版本后,运行效率快、线程不安全。
- 源码分析:
- DEFAULT_CAPACITY = 10;默认容量(注意:如果没有向集合中添加任何元素时,容量0;添加任意一个元素之后,容量10;每次扩容大小是原来的1.5倍)
- elementData 存放元素的数组
- size 实际元素个数
- add() 添加元素
| public boolean add(E e) { |
| ensureCapacityInternal(size + 1); |
| elementData[size++] = e; |
| return true; |
| } |
| private void ensureCapacityInternal(int minCapacity) { |
| ensureExplicitCapacity(calculateCapacity(elementData, minCapacity)); |
| } |
| private void ensureExplicitCapacity(int minCapacity) { |
| modCount++; |
| |
| |
| if (minCapacity - elementData.length > 0) |
| grow(minCapacity); |
| } |
| private void grow(int minCapacity) { |
| |
| int oldCapacity = elementData.length; |
| int newCapacity = oldCapacity + (oldCapacity >> 1); |
| if (newCapacity - minCapacity < 0) |
| newCapacity = minCapacity; |
| if (newCapacity - MAX_ARRAY_SIZE > 0) |
| newCapacity = hugeCapacity(minCapacity); |
| |
| elementData = Arrays.copyOf(elementData, newCapacity); |
| } |
Vector
- 数组结构实现,查询快、增删慢;
- JDK1.0版本,运行效率慢、线程安全。
| package com.collectionsFramework.demo02; |
| |
| import java.util.Enumeration; |
| import java.util.Vector; |
| |
| |
| |
| |
| |
| public class Demo01 { |
| public static void main(String[] args) { |
| |
| Vector vector = new Vector<>(); |
| |
| vector.add("草莓"); |
| vector.add("芒果"); |
| vector.add("西瓜"); |
| System.out.println("元素个数:"+vector.size()); |
| |
| |
| |
| |
| |
| |
| Enumeration en = vector.elements(); |
| while (en.hasMoreElements()){ |
| String o = (String) en.nextElement(); |
| System.out.println(o); |
| } |
| |
| vector.contains(vector.contains("西瓜")); |
| System.out.println(vector.isEmpty()); |
| |
| |
| |
| } |
| } |
LinkedList
- 链表结构实现,增删快,查询慢。
- 源码分析
- int size; 集合的大小
- Node first; 链表的头节点
- Node last; 链表的尾结点
| private void linkFirst(E e) { |
| final Node<E> f = first; |
| final Node<E> newNode = new Node<>(null, e, f); |
| first = newNode; |
| if (f == null) |
| last = newNode; |
| else |
| f.prev = newNode; |
| size++; |
| modCount++; |
| } |
| private static class Node<E> { |
| E item; |
| Node<E> next; |
| Node<E> prev; |
| |
| Node(Node<E> prev, E element, Node<E> next) { |
| this.item = element; |
| this.next = next; |
| this.prev = prev; |
| } |
| } |
例子:
| package com.collectionsFramework.demo02; |
| |
| import com.collectionsFramework.demo01.Student; |
| |
| import java.util.Iterator; |
| import java.util.LinkedList; |
| import java.util.ListIterator; |
| |
| |
| |
| |
| |
| public class Demo02 { |
| public static void main(String[] args) { |
| |
| LinkedList linkedList = new LinkedList<>(); |
| |
| Student s1 = new Student("刘德华", 20); |
| Student s2 = new Student("郭富城", 22); |
| Student s3 = new Student("梁朝伟", 18); |
| linkedList.add(s1); |
| linkedList.add(s2); |
| linkedList.add(s3); |
| |
| System.out.println("元素个数:"+linkedList.size()); |
| System.out.println(linkedList.toString()); |
| |
| |
| |
| |
| |
| |
| |
| System.out.println("---------for--------"); |
| for (int i = 0; i < linkedList.size(); i++) { |
| System.out.println(linkedList.get(i)); |
| } |
| |
| System.out.println("---------增强for--------"); |
| for (Object object:linkedList) { |
| Student s = (Student) object; |
| System.out.println(s.toString()); |
| } |
| |
| System.out.println("---------使用迭代器--------"); |
| Iterator it = linkedList.iterator(); |
| while (it.hasNext()){ |
| Student s = (Student) it.next(); |
| System.out.println(s.toString()); |
| } |
| |
| System.out.println("---------使用列表迭代器--------"); |
| ListIterator lit = linkedList.listIterator(); |
| while (lit.hasNext()){ |
| Student s = (Student) lit.next(); |
| System.out.println(s.toString()); |
| } |
| |
| System.out.println(linkedList.contains(s1)); |
| System.out.println(linkedList.isEmpty()); |
| |
| System.out.println(linkedList.indexOf(s1)); |
| } |
| } |
| package com.collectionsFramework.demo01; |
| |
| import java.util.Objects; |
| |
| |
| |
| |
| public class Student { |
| private String name; |
| private int age; |
| |
| public Student() { |
| } |
| |
| public Student(String name, int age) { |
| this.name = name; |
| this.age = age; |
| } |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| this.name = name; |
| } |
| |
| public int getAge() { |
| return age; |
| } |
| |
| public void setAge(int age) { |
| this.age = age; |
| } |
| |
| @Override |
| public String toString() { |
| return "Student{" + |
| "name='" + name + '\'' + |
| ", age=" + age + |
| '}'; |
| } |
| |
| @Override |
| public boolean equals(Object obj) { |
| |
| if (this==obj){ |
| return true; |
| } |
| |
| if (obj==null){ |
| return false; |
| } |
| |
| if (obj instanceof Student){ |
| Student s = (Student) obj; |
| |
| if (this.name.equals(s.getName())&&this.age==s.getAge()){ |
| return true; |
| } |
| } |
| |
| return false; |
| } |
| |
| } |
| package com.collectionsFramework.demo01; |
| |
| import java.util.ArrayList; |
| import java.util.Iterator; |
| import java.util.ListIterator; |
| |
| |
| |
| |
| |
| public class Demo05 { |
| public static void main(String[] args) { |
| |
| ArrayList arrayList = new ArrayList<>(); |
| |
| Student s1 = new Student("刘德华", 20); |
| Student s2 = new Student("郭富城", 22); |
| Student s3 = new Student("梁朝伟", 18); |
| arrayList.add(s1); |
| arrayList.add(s2); |
| arrayList.add(s3); |
| |
| System.out.println("元素个数:"+arrayList.size()); |
| System.out.println(arrayList.toString()); |
| |
| |
| |
| |
| |
| |
| |
| System.out.println("----------3.1使用迭代器--------"); |
| Iterator it = arrayList.iterator(); |
| while (it.hasNext()){ |
| Student s = (Student) it.next(); |
| System.out.println(s.toString()); |
| } |
| |
| System.out.println("----------3.2使用列表迭代器--------"); |
| ListIterator lit = arrayList.listIterator(); |
| while(lit.hasNext()){ |
| Student s = (Student) lit.next(); |
| System.out.println(s.toString()); |
| } |
| System.out.println("----------3.2使用列表迭代器逆序--------"); |
| while (lit.hasPrevious()){ |
| Student s = (Student) lit.previous(); |
| System.out.println(s.toString()); |
| } |
| |
| |
| System.out.println(arrayList.contains(s1)); |
| System.out.println(arrayList.contains(new Student("刘德华", 20))); |
| System.out.println(arrayList.isEmpty()); |
| |
| |
| System.out.println(arrayList.indexOf(s3)); |
| System.out.println(arrayList.indexOf(new Student("梁朝伟", 18))); |
| |
| } |
| } |
不同结构实现方法
ArrayList:必须开辟连续空间,查询快,增删慢。
LinkedList:无需开辟连续空间,查询慢,增删快。
笔记出处
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构