集合(Collection)
为什么不用数组而要用集合呢?
数组弊端:
1. 只能添加相同类型的元素(基本数据类型 和 引用数据类型)
2. 长度一旦确定就不能改变,要添加超出数组元素个数的元素 操作比较复杂
集合好处
1. 能添加不能类型的元素
注意:集合中只能添加 引用数据类型 (只能添加对象类型)
2. 长度可变
集合一些方法代码示例:
// rawtypes保持 原有类型 unchecked 不检查插入数据类型
// 放到类上 整个类不写泛型 都不会报黄
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo02 {
public static void main(String[] args) {
// 创建一个集合 多态的声明方法
//fun1();
//fun2();
//fun3();
//fun4();
//fun5();
//fun7();
}
/**
* removeAll
*/
public static void fun7() {
Collection collection = new ArrayList();
Collection collection1 = new ArrayList();
collection.add("a");
collection.add("b");
collection.add("c");
collection1.add("a");
collection1.add("y");
collection1.add("z");
// 把两个集合的交集取出来 保存在1集合中 谁调的方法保存在谁那
// 如果1集合和2集合 在求出交集时候 放到 1集合中 如果1集合和原来对比发生变化返回 true 如果不发生变化返回false
boolean removeAll = collection.retainAll(collection1);
System.out.println(collection);
System.out.println(collection1);
System.out.println(removeAll);
}
/**
*
*/
public static void fun5() {
Collection collection = new ArrayList();
Collection collection1 = new ArrayList();
collection.add("a");
collection.add("b");
collection.add("c");
collection1.add("a");
collection1.add("y");
collection1.add("z");
// 将两个集合的交集删掉 谁调用这个方法就删除谁的元素 另外的保持不变
// 只删除交集 重复也可以删
boolean removeAll = collection.removeAll(collection1);
System.out.println(collection);
System.out.println(collection1);
System.out.println(removeAll);
}
/**
* addAll 方法
*/
public static void fun4() {
Collection collection = new ArrayList();
Collection collection1 = new ArrayList();
collection.add("a");
collection.add("a");
collection1.add("ab");
collection1.add("c");
collection1.add("a");
//将2集合中的元素 每一个元素取出来 添加到1集合末尾
// 给集合1添加一个元素 元素是2集合
//collection.add(collection1);
collection.addAll(collection1);
System.out.println(collection);
System.out.println(collection1);
}
/**
* 集合强转
*/
public static void fun3() {
Collection collection = new ArrayList();
collection.add(new Student("james", 18));
collection.add(new Student("Tom", 18));
collection.add(new Student("jelly", 18));
// 集合转数组(向上转型)
Object[] array = collection.toArray();
//遍历数组
for (int i = 0; i < array.length; i++) {
// array[i] 直接从数组中取出来是Object类型 要使用 student类中的方法需要 向下转型 但是必须是这个类型才能强转
// 对象调方法 强转注意:把每一个数组中每一个对象进行强转 而不是把保存对象的容器(数组) 转化
Student student = (Student)array[i];
System.out.println(student.getName());
}
}
/**
* 打印字符串
*/
public static void fun2() {
Collection collection = new ArrayList();
collection.add("a");
collection.add("b");
collection.add("c");
collection.add("d");
// 遍历集合中的每一个元素
Object[] array = collection.toArray();
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
/**
* 集合的基本方法
* @param collection
*/
public static void fun1() {
//添加方法
Collection collection = new ArrayList();
collection.add("a");
collection.add("b");
collection.add("c");
// 当向集合中添加基本数据类型的时候 系统会自动装箱 把基本数据类型变成它的包装类的对象
collection.add(10);
System.out.println(collection);
// 获取集合的长度
System.out.println(collection.size());
// 判断是否包含某个元素
boolean contains = collection.contains("b");
System.out.println(contains);
// 从集合中删除一个元素
collection.remove("b");
//操作的是 原集合 是集合本身
// 打印集合
System.out.println(collection);
// 判断集合 是否为空
boolean empty = collection.isEmpty();
System.out.println(empty);
// 清空数组
collection.clear();
System.out.println(collection);
}
}
迭代器(遍历)
对 collection 进行迭代的迭代器
常用方法举例:
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo03 {
public static void main(String[] args) {
//fun1();
//fun2();
//fun3();
}
/**
*
*/
public static void fun3() {
Collection collection = new ArrayList();
collection.add(new Student("James", 18));
collection.add(new Student("Tom", 17));
collection.add(new Student("jelly", 19));
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
// 强转成student类
Student student =(Student)next;
System.out.println(student.getName());
}
}
/**
* 迭代遍历集合
*/
public static void fun2() {
Collection collection = new ArrayList();
// 迭代是 有一个指针 指向首位置
// 每调用一次next方法 指针向下移一格
collection.add("a");
collection.add("b");
collection.add("c");
collection.add("d");
Iterator iterator = collection.iterator();
// 如果有元素就继续获取 没元素停止循环
// 注意: 迭代时 循环中只能使用一次next() 方法
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println(next);
}
}
/**
* 迭代器
*/
public static void fun1() {
//测试迭代器中的方法
Collection collection = new ArrayList();
collection.add("a");
collection.add("b");
collection.add("c");
collection.add("d");
//获取集合中的迭代器
Iterator iterator = collection.iterator();
// 先判断集合中是否有元素
boolean hasNext = iterator.hasNext();
System.out.println(hasNext);
// 从集合中取出元素
Object next = iterator.next();
System.out.println(next);
}
}