Collection 集合
1. 数组和集合的区别
-
相同点
都是容器,可以存储多个数据
-
不同点
-
数组的长度是不可变的,集合的长度是可变的
-
数组可以存基本数据类型和引用类型
集合只能存引用数据类型,如果要存基本数据类型,需要存对应的包装类
-
2. 集合类体系结构
- 详细的体系结构可查看API文档
3. Collection 集合概述和使用
-
概述
- 是单列接口的顶层接口
- JDK 不提供此接口的任何直接实现,他提供更具体的子接口实现
-
用多态的方式出那个键Collection集合的对象
-
常用方法
方法名 说明 boolean add(E e) 添加元素 boolean remove(Object o) 从集合中移除指定的元素 boolean removeIf(Object o) 根据条件进行移除 void clear() 清空集合中的元素 boolean contains(Object o) 判断集合中是否存在指定的元素 boolean isEmpty() 判断集合是否为空 int size() 集合的大小,也就是集合中元素的个数
4. Collection 集合的遍历
-
迭代器
- Iterator iterator() :返回此集合中元素的迭代器,通过集合对象的iterator() 方法得到
-
Iterator 中的常用方法
方法名 说明 boolean hasNext() 判断当前位置是否有元素可以被取出 E next() 获取当前位置的元素,将迭代器对象移向下一个索引位置 void remove() 删除迭代器对象当前指向的元素 -
代码
public class IteratorDemo01 {
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
collection.add("a");
collection.add("b");
collection.add("c");
collection.add("d");
collection.add("e");
collection.add("e");
Iterator<String> it2 = collection.iterator();
while (it2.hasNext()){
String s = it2.next();
if ("e".equals(s)){
it2.remove();//指向谁就删除谁
}
}
Iterator<String> it = collection.iterator();
while (it.hasNext()){//判断当前索引位置有没有元素
String result = it.next();//获取当前位置的元素,并将迭代器移向下一个索引位置
System.out.print(result+" ");//a b c d
}
}
}
5. 增强for循环
-
概述
- 它是JDK5之后出现的,其内部原理是一个Iterator迭代器
- 实现Iterable接口的类才可以使用迭代器和增强for
-
格式
for(集合/数组中元素的数据类型 变量名 : 集合/数组名){ // 已经将当前遍历到的元素封装到变量中了,直接使用变量即可 }
-
代码
public class IteratorDemo02 {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("a");
arrayList.add("b");
arrayList.add("c");
arrayList.add("d");
//把每一个从arrayList中比那里出来的元素,赋值给变量s
for (String s : arrayList) {
System.out.print(s+" ");//a b c d
}
}
}
-
注意:在增强for中修改变量的值,不会对集合/数组造成影响
-
使用情况:
- 有需要操作索引时,使用普通for
- 要做删除时,使用迭代器
- 遍历和查看,使用增强for
-
代码:
public class IteratorDemo04 {
public static void main(String[] args) {
Collection<Student> collection = new ArrayList<>();
Student stu1 = new Student("张三",18);
Student stu2 = new Student("李四",20);
Student stu3 = new Student("王五",22);
collection.add(stu1);
collection.add(stu2);
collection.add(stu3);
for (int i = 0; i < collection.size(); i++) {
System.out.println(((ArrayList<Student>) collection).get(i));
}
System.out.println("----------------");
Iterator<Student> it = collection.iterator();
while (it.hasNext()){
Student student = it.next();
System.out.println(student);
}
System.out.println("----------------");
for (Student student : collection) {
System.out.println(student);
}
}
}
class Student{
private String name;
private Integer age;
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
List 集合
1. List集合的概述和特点
- List集合的概述
- 有序集合,这里的有序指的是存取顺序
- 用户可以精确控制列表中每个元素的插入位置,用户可以通过整数索引访问元素,并搜索列表中的元素
- 与Set集合不同,列表通常允许重复的元素
- List集合的特点
- 存取有序
- 可以重复
- 有索引
2. List集合的特有方法
方法名 | 描述 |
---|---|
void add(int index,E element) | 在指定位置插入指定的元素 |
E remove(int index) | 删除指定索引处的元素,返回被删除的元素 |
E set(int index,E element) | 修改指定索引处的元素,返回被修改的元素 |
E get(int index) | 返回指定索引处的元素 |
- 代码
public class ListDemo01 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
System.out.println(list);//[a, b, c, d]
String remove = list.remove(3);
System.out.println(remove);//d
System.out.println(list);//[a, b, c]
System.out.println("-----------");
String a = list.set(0, "A");
System.out.println(a);//a
System.out.println(list);//[A, b, c]
System.out.println("-----------");
String s = list.get(2);
System.out.println(s);//c
}
}
3. 数组和链表
数组 | 链表 |
---|---|
查询快:数组是连续有序存储,并提供下标索引,在查询时可以直接通过下标查询到对应元素 | 查询慢:链表没有索引值,在查询时只能从头或从尾开始遍历 |
增删慢:数组在删除或增加时,前后的元素可能都需要进行移动 | 增删快:链表在增加或者删除时,只要更改其前后单元的指针域的指向即可 |
4. List 集合的实现类
-
ArrayList集合
底层是数组结构实现,查询快、增删慢
-
LinkedList集合
底层是链表结构实现,查询慢、增删快
4.1 LinkedList集合的特有功能
方法名 | 说明 |
---|---|
public void addFirst(E e) | 在该列表开头插入指定的元素 |
public void addLast(E e) | 将指定的元素追加到此列表的末尾 |
public E getFirst() | 返回此列表中的第一个元素 |
public E getLast() | 返回此列表中的最后一个元素 |
public E removeFirst() | 从此列表中删除并返回第一个元素 |
public E removeLast() | 从此列表中删除并返回最后一个元素 |
public class LinkedListDemo01 {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("a");
linkedList.add("b");
linkedList.add("c");
linkedList.add("d");
System.out.println(linkedList);//[a, b, c, d]
linkedList.addFirst("111");
linkedList.addLast("222");
System.out.println(linkedList);//[111, a, b, c, d, 222]
System.out.println(linkedList.getFirst());//111
System.out.println(linkedList.getLast());//222
linkedList.removeFirst();
System.out.println(linkedList);//[a, b, c, d, 222]
linkedList.removeLast();
System.out.println(linkedList);//[a, b, c, d]
}
}