小白学集合之collceion
根据学习B站视频 Java集合框架详解整理:https://www.bilibili.com/video/BV1zD4y1Q7Fw?p=1
什么是集合?
-
集合概念:对象的容器,定义了对多个对象进行操作的常用方法。可实现数组的功能。
-
集合和数组的区别:
(1)数组长度固定;集合长度不固定。
(2)数组可以存储基本类型和引用类型;集合只能存储引用类型。
-
集合位置:
java.util.*
-
Collection体系结构
Collection父接口
特点:代表一组任意类型的对象,无序、无下标、不能重复。
方法:
boolean add(Object obj)
//添加一个对象
boolean addAll(Collection c)
//将一个集合中所有对象添加到此集合中
void clear()
//清空集合中所有对象
boolean contains(Object o)
//检查集合是否包含o对象
boolean equals(Object o)
//比较是否与指定集合相等
boolean isEmpty()
//判断集合是否为空
boolean remove(Object o)
//移除集合中o对象
int size()
//返回集合元素个数
Object [ ] toArray()
//将集合转换成数组
Collection集合
collection接口的简单使用,这个类是比较基础的一些操作。
接口不能new对象,所以要用它的实现类:new ArrayList<>();
注意:迭代过程中,不能用collection的remove()方法删除元素。
增强for的idea快捷键:iter,然后回车。
迭代器的idea快捷键:itit,然后回车。
package com.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* Collection接口的使用
* 1、创建
* 2、添加元素
* 3、删除元素
* 4、遍历元素
* 5、判断
*/
public class CollectionDemo {
public static void main(String[] args) {
//1、创建集合
Collection collection = new ArrayList<>();
//2、添加元素
collection.add("苹果");
collection.add("香蕉");
collection.add("梨");
System.out.println("元素个数:" + collection.size());
System.out.println(collection);
//3、删除元素
// collection.remove("梨");
// System.out.println("删除后元素个数:"+collection.size());
//4、遍历元素【重点】
//4.1、增强for遍历。
//idea快捷键:iter,然后回车。
for (Object o : collection) {
System.out.println("增强for遍历:" + o);
}
//4.2、迭代器遍历(专门用来遍历集合的一种方式)
//hasNext();有没有下一个元素
//next();读取下一个元素
//remove();删除当前元素
Iterator iterator = collection.iterator();
//idea快捷键:itit,然后回车
while (iterator.hasNext()) {
String next = (String) iterator.next();
System.out.println("迭代器遍历:" + next);
//注意:迭代过程中,不能用collection的remove()方法删除元素
//collection.remove(next); //会报并发修改异常
//真想删除可以使用迭代器的删除方法:
iterator.remove(); //遍历一个删除一个,遍历完成,删除完
}
System.out.println("元素个数:" + collection.size());
//判断是否为空
System.out.println("是否为空:" + collection.isEmpty());
}
}
Collection的使用:保存学生信息:比较基础的一些操作,
package com.collection;
import java.time.zone.ZoneOffsetTransitionRule;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* Collection的使用:保存学生信息
*/
public class CollectionDemo2 {
public static void main(String[] args) {
//1、创建Collection对象
Collection collection = new ArrayList();
Student s1 = new Student("张三", 18);
Student s2 = new Student("李四", 20);
Student s3 = new Student("王二", 22);
//2、添加数据
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println("元素个数:" + collection.size());
System.out.println(collection.toString());
//3、删除
//collection.remove(s1); //删除s1
//collection.clear(); //全部删除
//4、遍历
//4.1、增强for 快捷键:iter
for (Object o : collection) {
Student s = (Student) o;
System.out.println("增强for:" + s.toString());
}
//4.2、迭代器:hasNext() next() remove().
//迭代过程中不能使用collection的删除方法
Iterator it = collection.iterator();
//快捷键:itit
while (it.hasNext()) {
Student next = (Student) it.next();
System.out.println("迭代器"+next.toString());
}
}
}
student类
这个类是配合上边的collection类,因为上边添加元素是student对象。
package com.collection;
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 +
'}';
}
}