集合类的特点
提供一种存储空间可变的存储模型,存储的数据容量可以随时发生改变
集合类的体系图
Collection集合概述
是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素
JDK 不提供此接口的任何直接实现,它提供更具体的子接口(如Set和List)实现
Collection集合基本使用
1 public class CollectionDemo01 { 2 public static void main(String[] args) { 3 //创建Collection集合的对象 4 Collection<String> c = new ArrayList<String>(); 5 6 //添加元素:boolean add(E e) 7 c.add("hello"); 8 c.add("world"); 9 c.add("java"); 10 11 //输出集合对象 12 System.out.println(c); 13 } 14 }
方法名 | 说明 |
---|---|
boolean add(E e) | 添加元素 |
boolean remove(Object o) | 从集合中移除指定的元素 |
void clear() | 清空集合中的元素 |
boolean contains(Object o) | 判断集合中是否存在指定的元素 |
boolean isEmpty() | 判断集合是否为空,为空true |
int size() | 集合的长度,也就是集合中元素的个数 |
迭代器的介绍
迭代器,集合的专用遍历方式
Iterator<E> iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到
迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖于集合而存在的
Collection集合的遍历
1 public class IteratorDemo { 2 public static void main(String[] args) { 3 //创建集合对象 4 Collection<String> c = new ArrayList<>(); 5 6 //添加元素 7 c.add("hello"); 8 c.add("world"); 9 c.add("java"); 10 c.add("javaee"); 11 12 //Iterator<E> iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到 13 Iterator<String> it = c.iterator(); 14 15 //用while循环改进元素的判断和获取 16 while (it.hasNext()) { 17 String s = it.next(); 18 System.out.println(s); 19 } 20 } 21 }
使用步骤
案例需求
创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合
代码实现
学生类
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; } }
测试类
1 public class CollectionDemo { 2 public static void main(String[] args) { 3 //创建Collection集合对象 4 Collection<Student> c = new ArrayList<Student>(); 5 6 //创建学生对象 7 Student s1 = new Student("林青霞", 30); 8 Student s2 = new Student("张曼玉", 35); 9 Student s3 = new Student("王祖贤", 33); 10 11 //把学生添加到集合 12 c.add(s1); 13 c.add(s2); 14 c.add(s3); 15 16 //遍历集合(迭代器方式) 17 Iterator<Student> it = c.iterator(); 18 while (it.hasNext()) { 19 Student s = it.next(); 20 System.out.println(s.getName() + "," + s.getAge()); 21 } 22 } 23 }
List集合概述
有序集合(也称为序列),用户可以精确控制列表中每个元素的插入位置。用户可以通过整数索引访问元素,并搜索列表中的元素
与Set集合不同,列表通常允许重复的元素
List集合特点
有索引
可以存储重复元素
元素存取有序
方法名 | 描述 |
---|---|
void add(int index,E element) | 在此集合中的指定位置插入指定的元素 |
E remove(int index) | 删除指定索引处的元素,返回被删除的元素 |
E set(int index,E element) | 修改指定索引处的元素,返回被修改的元素 |
E get(int index) | 返回指定索引处的元素 |
案例需求
创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合
代码实现
学生类
1 public class Student { 2 private String name; 3 private int age; 4 5 public Student() { 6 } 7 8 public Student(String name, int age) { 9 this.name = name; 10 this.age = age; 11 } 12 13 public String getName() { 14 return name; 15 } 16 17 public void setName(String name) { 18 this.name = name; 19 } 20 21 public int getAge() { 22 return age; 23 } 24 25 public void setAge(int age) { 26 this.age = age; 27 } 28 }
测试类
public class ListDemo { public static void main(String[] args) { //创建List集合对象 List<Student> list = new ArrayList<Student>(); //创建学生对象 Student s1 = new Student("林青霞", 30); Student s2 = new Student("张曼玉", 35); Student s3 = new Student("王祖贤", 33); //把学生添加到集合 list.add(s1); list.add(s2); list.add(s3); //迭代器方式 Iterator<Student> it = list.iterator(); while (it.hasNext()) { Student s = it.next(); System.out.println(s.getName() + "," + s.getAge()); } System.out.println("--------"); //for循环方式 for(int i=0; i<list.size(); i++) { Student s = list.get(i); System.out.println(s.getName() + "," + s.getAge()); } } }
出现的原因
迭代器遍历的过程中,通过集合对象修改了集合中的元素,造成了迭代器获取元素中判断预期修改值和实际修改值不一致,则会出现:ConcurrentModificationException
解决的方案
用for循环遍历,然后用集合对象做对应的操作即可
示例代码
1 public class ListDemo { 2 public static void main(String[] args) { 3 //创建集合对象 4 List<String> list = new ArrayList<String>(); 5 6 //添加元素 7 list.add("hello"); 8 list.add("world"); 9 list.add("java"); 10 11 //遍历集合,得到每一个元素,看有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现 12 // Iterator<String> it = list.iterator(); 13 // while (it.hasNext()) { 14 // String s = it.next(); 15 // if(s.equals("world")) { 16 // list.add("javaee"); 17 // } 18 // } 19 20 for(int i=0; i<list.size(); i++) { 21 String s = list.get(i); 22 if(s.equals("world")) { 23 list.add("javaee"); 24 } 25 } 26 27 //输出集合对象 28 System.out.println(list); 29 } 30 }
ListIterator介绍
通过List集合的listIterator()方法得到,所以说它是List集合特有的迭代器
用于允许程序员沿任一方向遍历的列表迭代器,在迭代期间修改列表,并获取列表中迭代器的当前位置
public class ListIteratorDemo { public static void main(String[] args) { //创建集合对象 List<String> list = new ArrayList<String>(); //添加元素 list.add("hello"); list.add("world"); list.add("java"); //获取列表迭代器 ListIterator<String> lit = list.listIterator(); while (lit.hasNext()) { String s = lit.next(); if(s.equals("world")) { lit.add("javaee"); } } System.out.println(list); } }
for(元素数据类型 变量名 : 数组/集合对象名) {
循环体;
}
示例代码
public class ForDemo { public static void main(String[] args) { int[] arr = {1,2,3,4,5}; for(int i : arr) { System.out.println(i); } System.out.println("--------"); String[] strArray = {"hello","world","java"}; for(String s : strArray) { System.out.println(s); } System.out.println("--------"); List<String> list = new ArrayList<String>(); list.add("hello"); list.add("world"); list.add("java"); for(String s : list) { System.out.println(s); } System.out.println("--------"); //内部原理是一个Iterator迭代器 /* for(String s : list) { if(s.equals("world")) { list.add("javaee"); //ConcurrentModificationException } } */ } }
案例需求
创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合
代码实现
学生类
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; } }
测试类
1 public class ListDemo { 2 public static void main(String[] args) { 3 //创建List集合对象 4 List<Student> list = new ArrayList<Student>(); 5 6 //创建学生对象 7 Student s1 = new Student("林青霞", 30); 8 Student s2 = new Student("张曼玉", 35); 9 Student s3 = new Student("王祖贤", 33); 10 11 //把学生添加到集合 12 list.add(s1); 13 list.add(s2); 14 list.add(s3); 15 16 //迭代器:集合特有的遍历方式 17 Iterator<Student> it = list.iterator(); 18 while (it.hasNext()) { 19 Student s = it.next(); 20 System.out.println(s.getName()+","+s.getAge()); 21 } 22 System.out.println("--------"); 23 24 //普通for:带有索引的遍历方式 25 for(int i=0; i<list.size(); i++) { 26 Student s = list.get(i); 27 System.out.println(s.getName()+","+s.getAge()); 28 } 29 System.out.println("--------"); 30 31 //增强for:最方便的遍历方式 32 for(Student s : list) { 33 System.out.println(s.getName()+","+s.getAge()); 34 } 35 } 36 }
数组结构
查询快、增删慢
链表结构
ArrayList集合
底层是数组结构实现,查询快、增删慢
LinkedList集合
底层是链表结构实现,查询慢、增删快
创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合
代码实现
学生类
1 public class Student { 2 private String name; 3 private int age; 4 5 public Student() { 6 } 7 8 public Student(String name, int age) { 9 this.name = name; 10 this.age = age; 11 } 12 13 public String getName() { 14 return name; 15 } 16 17 public void setName(String name) { 18 this.name = name; 19 } 20 21 public int getAge() { 22 return age; 23 } 24 25 public void setAge(int age) { 26 this.age = age; 27 } 28 }
测试类
1 public class ArrayListDemo { 2 public static void main(String[] args) { 3 //创建ArrayList集合对象 4 ArrayList<Student> array = new ArrayList<Student>(); 5 6 //创建学生对象 7 Student s1 = new Student("林青霞", 30); 8 Student s2 = new Student("张曼玉", 35); 9 Student s3 = new Student("王祖贤", 33); 10 11 //把学生添加到集合 12 array.add(s1); 13 array.add(s2); 14 array.add(s3); 15 16 //迭代器:集合特有的遍历方式 17 Iterator<Student> it = array.iterator(); 18 while (it.hasNext()) { 19 Student s = it.next(); 20 System.out.println(s.getName() + "," + s.getAge()); 21 } 22 System.out.println("--------"); 23 24 //普通for:带有索引的遍历方式 25 for(int i=0; i<array.size(); i++) { 26 Student s = array.get(i); 27 System.out.println(s.getName() + "," + s.getAge()); 28 } 29 System.out.println("--------"); 30 31 //增强for:最方便的遍历方式 32 for(Student s : array) { 33 System.out.println(s.getName() + "," + s.getAge()); 34 } 35 } 36 }
特有方法
说明 | |
---|---|
public void addFirst(E e) | 在该列表开头插入指定的元素 |
public void addLast(E e) | 将指定的元素追加到此列表的末尾 |
public E getFirst() | 返回此列表中的第一个元素 |
public E getLast() | 返回此列表中的最后一个元素 |
public E removeFirst() | 从此列表中删除并返回第一个元素 |
public E removeLast() |
原文:https://www.cnblogs.com/shansir/p/13912861.html