集合
数组的弊端:
1.只能添加相同类型的元素(基本数据类型 和 引用数据类型 都能保存)
2.长度一旦确定 就不能改变 要添加超出数组长度个数的元素 操作比较复杂
集合由来 因为数组操作数据的弊端 用来代替数组
集合特点:
1.能添加不同类型的元素
注意: 集合中只能添加 引用数据类型(只能添加 对象类型)
2.长度可变
集合collection接口中的方法
集合中的基本方法
没写泛型的时候
将@SuppressWarnings ({ "rawtypes" , "unchecked" })
放到类 上
整个类都不会报黄
集合可以保存不同数据类型的数据
保存基本数据类型 是以自动装箱的形式 进行储存
// 创建一个集合 多态的声明方式
Collection collection = new ArrayList()
//添加方法
//什么时候会添加失败?
//ArrayList中的add 不可能返回失败
//不能返回失败 为什么还要设计返回值? --- 思想
//适用所有的子类 子接口
boolean b1 = collection.add ("a" )
boolean b2 = collection.add ("b" )
boolean b3 = collection.add ("c" )
// 当你向集合当中添加基本数据类型的时候
// 系统会帮你进行 自动装箱 把基本数据类型 变成 它的包装类
boolean b4 = collection.add (10 )
boolean b5 = collection.add (true)
boolean b6 = collection.add ('s' )
//打印集合
System.out .println (collection.toString ())
// System.out .println (b1)
// System.out .println (b2)
// System.out .println (b3)
// System.out .println (b4)
// System.out .println (b5)
// System.out .println (b6)
//获取集合的长度
System.out .println (collection.size ())
// 判断是否包含某个元素
boolean b7 = collection.contains ("b" )
System.out .println (b7)
//从集合中删除一个元素
boolean b8 = collection.remove (true)
System.out .println (b8)
// 操作的是 集合本身
System.out .println (collection)
//判断集合 是否为空
boolean b9 = collection.isEmpty ()
System.out .println (b9)
//清空数组
collection.clear ()
System.out .println (collection)
添加
public static void fun2 () {
Collection collection = new ArrayList();
collection.add("a" );
collection.add("b" );
collection.add("c" );
collection.add("d" );
System.out .println(collection);
Object[] array = collection.toArray();
for (int i = 0 ; i < array.length; i++) {
System.out .println(array[i]);
}
System.out .println(Arrays.toString(array));
}
强制转换
前提: 需要创建一个学生类 有名字 和 年龄
下面的Student就是创建出来的类
注意学生类的成员变量私有化 构造函数 set /get 方法都要写
public static void fun3 () {
Collection collection = new ArrayList();
collection.add(new Student("sxm" , 15 ));
collection.add(new Student("ygs" , 16 ));
collection.add(new Student("xsd" , 17 ));
Object[] array = collection.toArray();
for (int i = 0 ; i < array.length; i++) {
Student student = (Student)array[i];
System.out .println(student.getName());
}
}
addAll方法
Collection collection = new ArrayList()
collection.add ("ygs" )
collection.add ("sxm" )
collection.add ("dp" )
//addAll是把前一个集合的东西 都放进这个数组里
Collection collection2 = new ArrayList()
collection2.add ("y" )
collection2.add ("s" )
collection2.add ("d" )
collection2.addAll (collection)
//给集合collection2 添加一个元素 这个元素是 collection
//collection2.add (collection)
//打印结果 [y , s, d, ygs, sxm, dp, [ygs, sxm, dp]]
System.out .println (collection)
System.out .println (collection2)
containsAll方法
Collection collection = new ArrayList()
collection.add ("ygs" )
collection.add ("sxm" )
collection.add ("dp" )
//containsAll 就是 前一个集合的元素和这个元素都一样就返回true
Collection collection2 = new ArrayList()
collection2.add ("ygs" )
collection2.add ("sxm" )
collection2.add ("dp" )
boolean containsAll = collection2.containsAll (collection)
System.out .println (containsAll)
removeAll方法
Collection collection = new ArrayList()
collection.add ("ygs" )
collection.add ("sxm" )
collection.add ("dp" )
//removeAll
//这个方法 将两个集合的重合元素删除
//谁调用这个方法 就删除 谁的元素 另一个集合 不变
//只删除交集 重复也可以删
Collection collection2 = new ArrayList()
collection2.add ("ygs" )
collection2.add ("sxm" )
collection2.add ("dp" )
collection2.add ("wl" )
collection2.removeAll (collection)
System.out .println (collection2)
retainAll
Collection collection = new ArrayList()
collection.add ("ygs" )
collection.add ("sxm" )
collection.add ("dp" )
//retainAll
// 把两个集合的交集取出来 保存在collection2中(谁调方法保存在谁那)
// 如果collection 和 collection2 求出交集 放到 collection2中
// 如果collection2和原来对比 发生变化 返回 true
// 不发生变化 返回false
Collection collection2 = new ArrayList()
collection2.add ("ygs" )
collection2.add ("sxm" )
collection2.add ("dp" )
collection2.add ("wl" )
boolean retainAll = collection2.retainAll (collection)
System.out .println (collection2)
System.out .println (retainAll)
}
迭代器(遍历)
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 isHasNext = iterator.hasNext();
System.out .println(isHasNext);
Object next = iterator.next();
System.out .println(next);
}
遍历集合
public static void fun2 () {
Collection collection = new ArrayList();
collection.add("a" );
collection.add("b" );
collection.add("c" );
collection.add("d" );
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
System.out .println(iterator.next());
}
}
强制类型转换
public static void fun3 () {
Collection collection = new ArrayList();
collection.add(new Student("sxm" ,18 ));
collection.add(new Student("ygs" ,19 ));
collection.add(new Student("dp" ,20 ));
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
Student student = (Student)next;
System.out .println(student.getName());
}
}
Day.15