集合
一、集合的概念
- 集合是对象的容器,定义了多个对象进行操作的常用方法;可以实现数组的功能;
- 和数组的区别:
- 数组长度固定,但集合长度不固定;
- 数组可以存储基本类型和引用类型,但是集合只能储存引用类型;(有基本类型需求可以进行装箱处理;
Collection接口
一、Collection父接口
- 特点:代表一组任意类型的对象,无序、无下标、不能重复;
- 方法:
方法名 | 说明 |
---|---|
boolean add(Object obj) | 添加一个对象 |
boolean addALL(Collection c) | 将一个集合中的所有对象添加到此集合中 |
void clear() | 清空此集合中的所有对象 |
boolean contains(Object o) | 检查此集合中是否包含o对象 |
boolean equals(Object o) | 比较此集和是否与指定对象o相等 |
boolean isEmpty() | 判断此集合是否为空 |
boolean remove(Object o) | 在此集合中移除o对象 |
int size() | 返回此集合中的元素个数 |
Object[] toArray() | 将此集合转换成数组 |
iterator() | 返回在此集合的元素上机型迭代的迭代器 |
- 关于迭代器 iterator():
- 是对集合进行遍历的一种方法;
- hasNext():如果仍有元素可以迭代,则返回true;
- next():返回迭代的下一个元素;
- remove():从迭代器指向的collection中移除迭代器返回的最后一个元素;
使用实例:
public class Demo01 {
public static void main(String[] args){
//创建集合
Collection collection = new ArrayList();
//1.添加元素
System.out.println("=====添加元素=====");
collection.add("1");
collection.add("2");
collection.add("3");
collection.add("4");
collection.add("5");
collection.add("6");
collection.add("7");
collection.add("8");
System.out.println(collection.size());
System.out.println(collection);
//2.删除元素
System.out.println("=====删除元素=====");
collection.remove("8");
// collection.clear();//清除集合内元素
System.out.println(collection.size());
System.out.println(collection);
//3.遍历元素【重点】
System.out.println("=====遍历元素=====");
//3.1使用增强for
System.out.println("=====使用增强for=====");
for (Object object:collection) {
System.out.println(object);
}
//3.2使用迭代器(专门用来遍历集合的一种方式)
System.out.println("=====使用迭代器=====");
Iterator it = collection.iterator();
while(it.hasNext()){
String object = (String)it.next();
System.out.println(object);
// it.remove();//迭代器中不允许使用collection的删除方法
}
System.out.println("元素个数为:"+collection.size());
//4.判断元素
System.out.println("=====判断元素=====");
System.out.println(collection.contains("3"));
System.out.println(collection.isEmpty());
}
}
public class Student {
private String name;
private int age;
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 + '}';
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo02 {
public static void main(String[] args){
//新建一个collection对象
Collection collection = new ArrayList();
Student s1 = new Student("赵大",21);
Student s2 = new Student("吴二",30);
Student s3 = new Student("张三",16);
Student s4 = new Student("李四",25);
Student s5 = new Student("王五",24);
//1.添加数据
collection.add(s1);
collection.add(s2);
collection.add(s3);
collection.add(s4);
collection.add(s5);
System.out.println("集合中元素个数为:"+collection.size());
System.out.println(collection.toString());
//2.删除数据
collection.remove(s2);
// collection.clear();//清空集合中的元素,只是清空了集合中的对象,对象在堆中依然存在;
System.out.println("删除之后的集合中元素个数为:"+collection.size());
//3.遍历数据
//3.1使用增强for
System.out.println("=====使用增强for=====");
for (Object object:collection) {
Student s = (Student)object;
System.out.println(s);
}
//3.2使用迭代器
System.out.println("=====使用迭代器=====");
Iterator it = collection.iterator();
while (it.hasNext()){
Student s = (Student)it.next();
System.out.println(s);
}
//4.判断数据
System.out.println(collection.contains(s1));
System.out.println(collection.isEmpty());
}
}