java集合

java集合

介绍:
1.集合与数组
数组弊端:
1.只能添加相同数据类型的元素
2.长度一旦确定就不能改变 要添加超出数组长度的元素 操作比较复杂

集合诞生:
1.源于数组的弊端,集合诞生了
2.集合可以添加不同类型的元素
3.集合长度可变
集合弊端:
集合只能添加引用数据类型(只能添加对象类型)
2.集合演练
2.1创建Student类
public class Student {
private String name;
private int age;
public Student() {
    super();
    // TODO Auto-generated constructor stub
}
public Student(String name, int age) {
    super();
    this.name = name;
    this.age = age;
}
public Student(String name) {
    super();
    this.name = name;
}
public Student(int age) {
    super();
    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 "姓名" + name + "...." + "年龄" + age;

}
}
注释:重写tostring方法专门用来输出对象信息
2.2利用数组输出学生信息
public class Demo02 {
    public static void main(String[] args) {
        Student[] students  = new Student[3];保存学生的数组
        students[0] = new Student(" 姓名1",12);
        students[1] = new Student(" 姓名2",12);
        students[2] = new Student(" 姓名3",12);

        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i]);

        }
    }

}

运行结果:
姓名 姓名1....年龄12
姓名 姓名2....年龄12
姓名 姓名3....年龄12
2.3利用集合输出学生信息
public class tesk {
   public static void main(String[] args) {
        Collection collection = new ArrayList();
        collection.add(new Student("姓名1", 18));
        collection.add(new Student("姓名2", 17));
        collection.add(new Student("姓名3", 16));
        //  集合转数组(相当于 有一个 向上转型)
        //  对象调方法 强转注意: 把数组中每一个对象进行强转
        //  而不是把保存对象的容器(数组) 转化
        Object array[] = collection.toArray();
        System.out.println(array);
        for (int i = 0; i < array.length; i++) {
            System.out.println(((Student) array[i]).getName());
        }
}
}

运行结果:
Ljava.lang.Object;@33909752
姓名1
姓名2
姓名3
注释:
1.集合转数组(相当于 有一个 向上转型)
2.对象调方法 强转注意: 把数组中每一个对象进行强转
        //  而不是把保存对象的容器(数组) 转化
3.array对应的是一个地址
@SuppressWarnings({"rawtypes","unchecked"}) //放在类上整个类不报黄 
3.ArrayList中的add
public class tesk {
   public static void main(String[] args) {
        Collection collection =new ArrayList();
        Student[] students  = new Student[5];
        boolean b1 = collection.add("a");
        collection.add("b");
        collection.add("c");
        collection.add(10);
        collection.add(true);
        collection.add('a');
        collection.add(null);
        collection.add(students[4]);

        System.out.println(collection.toString());
        System.out.println(b1);
运行结果:
[a, b, c, 10, true, a, null, null]
true
结论:
1.ArrayList中的add 不可能返回失败
2.适用所有的子类 子接口
3.当你向集合中添加基本数据类型的时候,系统会进行自动装箱 把基本数据类型变成他的包装类


//      获取集合的长度
        System.out.println(collection.size());
//      判断是否包含某个元素
        boolean b7 = collection.contains("aa");
        System.out.println(b7);
运行结果:
8
false

//      从集合中删除一个元素
        boolean b8 = collection.remove("b");
        System.out.println(b8);
    //  操作的是原集合
        System.out.println(collection);
运行结果:
true
[a, c, 10, true, a, null, null]

//      判断集合是否为空
        boolean b9 = collection.isEmpty();
        System.out.println(b9);
//      清空数组
        collection.clear();
        System.out.println(collection);
}
}
运行结果:
false
[]

4.集合collection接口中的方法
4.1 collection.remove的作用
public class tesk {
   public static void main(String[] args) {
       Collection collection = new ArrayList();
        collection.add("a");
        collection.add("b");
        collection.add("c");

        Collection collection1 = new ArrayList();
        collection1.add("a");
        collection1.add("b");
        collection1.add("2");
        collection.add("c");

        boolean s = collection.remove(collection1);
        System.out.println(s);
        System.out.println(collection);
        System.out.println(collection1);
}
}

运行结果: 
false
[a, b, c, c]
[a, b, 2]
结论: 
1.将两个集合多出来的字符拼接到当前集合另一个
集合减少多多出来的字符
2.填入一个字符着会删除当前集合对应的字符,并将另一个集合多出来的字符拼截取接到当前字符
5.1 collection.removeAll的作用
public class tesk {
   public static void main(String[] args) {
        Collection collection = new ArrayList();
        collection.add("a");
        collection.add("b");
        collection.add("c");
        collection.add("c");

        Collection collection1 = new ArrayList();
        collection1.add("a");
        collection1.add("b");
        collection1.add("2");


        boolean b = collection.removeAll(collection1);
        System.out.println(b);
        System.out.println(collection);
}
}

运行结果:
true
[c, c]
结论:
1.将两个集合重合的元素删除
谁调用这个方法 就删除谁的元素 另一个集合不变
2.只删除交集 重复也可以删
7.1 collection.retainAll的作用
        Collection collection = new ArrayList();
        collection.add("a");
        collection.add("b");
        collection.add("c");

        Collection collection1 = new ArrayList();
        collection1.add("a");
        collection1.add("b");
        collection1.add("2");


        boolean n = collection.retainAll(collection1);
        System.out.println(n);
        System.out.println(collection);
        System.out.println(collection1);
}
}

运行结果:
true
[a, b]
[a, b, 2]
结论:
        把两个集合的交集取出来 保存在c1(谁调方法保存在谁哪)
        如果collection集合和collection1集合 求出交集 放到c1中
        如果collection和原来对比 发生变化返回true反之为false
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值