集合 : Collection
数组和集合的区别:
数组:
a.数组只能存相同数据类型的数据,可以存引用数据类型和基本数据类型;
b.数组一旦给定长度就不能改变长度了;
集合:
1.注意:集合中只能存引用数据类型(对象),也可以通过装箱来保存基本数据类型;
2.集合的长度可以发生变化;
当数据固定的时候可以用数组;
当数据不固定的时候可以用集合;
public class Student {
private String name;
private int age;
//构造方法
public Student() {
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
//set/get方法
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//重写toString方法
//直接打印student对象,就可以调用toString方法;
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
public class demo{
public static void main (String[] args){
fun1;
sun2;
fun3;
fun4;
fun5;
fun6;
fun7;
fun8;
}
public static void fun1() {
//测试collection接口中的方法
Collection collection = new ArrayList();
collection.add("a");
collection.add(true);
collection.add(100);
collection.add(new Student("李宁",40));
System.out.println(collection);
//打印结果:[a, true, 100, Student [name=李宁, age=18]]
}
public static void fun2(){
Collection collection = new ArrayList;
collection.add("a");
collection.add("b");
collection.add("c");
collection.add("d");
//判断这个集合是否为空
boolean b1 = collection.isEmpty();
System.out.println(b1);
//打印结果:false
//获取集合长度
System.out.println(collection.size());
//打印结果:4
//判断是否包含某个元素
boolean b2 = collection.contains("z");
System.out.println(b2);
//打印结果:false
//删除某个元素
boolean b3 = collection.remove("a");
System.out.println(b3);
//打印结果:true;
//清空集合
collection.clear();
//打印集合元素
System.out.println(collection)
}
public static void fun3(){
Collection collection = new ArrayList;
collection.add("a");
collection.add("b");
collection.add("c");
collection.add("d");
collection.toArray();
Objecr[] array = collection.toArray();
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
}
public static void fun4(){
Collection collection = new ArrayList();
collection.add(new Student("刘备",11));//向上转型
collection.add(new Student("张飞",12));
collection.add(new Student("",13));
//集合转数组
Object[] array = collection.toArray();
for (int i = 0; i < array.length; i++) {
//向下转型
Student student = (Student)array[i];
System.out.println(student.getName());
}
}
//将集合里面所有的元素添加到另外一个集里面: 集合1名.addAll(集合2名);
public static void fun5(){
//声明一个集合
Collection c1 = new Collection();
//添加集合里面的元素
c1.add("a");
c1.add("b");
c1.add("c");
c1.add("b");
//再创建一个集合 c2;
Collection c2 = new Collection();
//创建c2里面的元素
c2.add("e");
c2.add("f");
//将c2里面的所有元素添加到c1里面"addAll"
c1.addAll(c2);
System.out.println(c1);
//打印c1长度
System.out.println(c1.size());
}
//判断两个集合是否有重复元素
public static void fun6() {
Collection c1 = new ArrayList();
c1.add("a");//
c1.add("b");
c1.add("c");
c1.add("d");
Collection c2 = new ArrayList();
c2.add("a");
c2.add("b");
c2.add("b");//判断全部包含是是可以有重复元素的
boolean b = c1.containsAll(c2);
System.out.println(b);
}
public static void fun7() {
Collection c1 = new ArrayList();
c1.add("a");//
c1.add("b");
c1.add("c");
c1.add("d");
Collection c2 = new ArrayList();
c2.add("a");
c2.add("b");
c2.add("www");
//判断全部包含是是可以有重复元素的
//相当于把c1和c2的交集,在c1集合中删除
//删除时,可以有不相同的元素
c1.removeAll(c2);
System.out.println(c1);
System.out.println(c2);
}
public static void fun8() {
Collection c1 = new ArrayList();
c1.add("a");//
c1.add("b");
c1.add("c");
c1.add("d");
Collection c2 = new ArrayList();
c2.add("a");
c2.add("v");
c2.add("x");
c2.add("x");
//c1转换成两个集合的交集取出来给
//如果c1改变了 就返回true
//c1没改变返回false
boolean b = c1.retainAll(c2);
System.out.println(b);
System.out.println(c1);
}
}
迭代器
public class Demo02 {
public static void main(String[] args) {
//迭代器遍历时相当于有一个指针指向你的集合
//每next一次指针就向后移动
//最后遍历完成
Collection collection = new ArrayList();
collection.add("a");//相当于有个指针
collection.add("b");
collection.add("c");
collection.add("d");
fun1(collection);
//使用迭代器循环遍历数组
//获取集合中的迭代器
fun2();
//向集合中添加三个学生进行遍历(使用迭代器)
Collection collection = new ArrayList();
collection.add(new Student("linig",56));
collection.add(new Student("dfj",54));
collection.add(new Student("dhfjf",56));
//获取集合中迭代器
Iterator iterator = collection.iterator();
//循环遍历
while (iterator.hasNext()==true) {
//next返回值是基础类
//获取集合中的元素一定要会
//向下转型
Student student = (Student)iterator.next();
System.out.println(student.getName());
}//重点iterator两个方法:hasNext\remove
}
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()==true) {
//获取集合中的元素一定要会
System.out.println(iterator.next());
}
}
public static void fun1(Collection collection) {
//获取集合中的迭代器
Iterator iterator = collection.iterator();
//判断有没有下一个元素
boolean b1 = iterator.hasNext();
System.out.println(b1);
//获取下一个元素
Object next = iterator.next();
System.out.println(next);
}
}