对象数组、集合、数据结构

对象数组、集合、数据结构

对象数组

1.案例:需求:我有3个学生,请把这个3个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。
public class Student {
    private String name;
    private int age;

    public Student() {
    }

    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 +
                '}';
    }
}
public class MyTest1 {
    public static void main(String[] args) {
        // 需求:我有3个学生,请把这个3个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。
        //数组,不仅能存储,基本数据类型,也能存储存储引用数据类型
        //对象数组
        Student[] students = new Student[3];
        students[0]=new Student("张三",23);
        students[1]=new Student("李四",24);
        students[2]=new Student("王五",25);
        //遍历数组
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            System.out.println(student.getName()+"---"+student.getAge());
        }
    }
}
  数组作为容器,可存储元素,但是数组作为容器,不是那么的方便,比如对容器中的元素进行增删,就不方便。
  因为数组一旦定义,长度就固定了,在途中如果增删,就会改变数组的长度,就会报错
  所以说,Java为了更加方便的对容器中的元素,进行更加方便的操作,提供了另外一种容器,叫做集合。
  使用集合,对集合中的元素进行增删改查,就比较方便。


Collection集合

1.集合和数组之间的区别:
    (1): 长度区别:
		数组的长度是固定的而集合的长度是可变的
	(2): 存储数据类型的区别:
		数组可以存储基本数据类型 , 也可以存储引用数据类型; 而集合只能存储引用数据类型
	(3): 内容区别:
		数组只能存储同种数据类型的元素 ,集合可以存储不同类型的元素
2.Collection集合:
  Collection的功能概述(通过API查看即可得到)
	a:添加功能
		boolean add(Object obj):添加一个元素
		boolean addAll(Collection c):添加一个集合的元素  (给一个集合添加进另一个集合中的所有元素)
	b:删除功能
		void clear():移除所有元素
		boolean remove(Object o):移除一个元素
		boolean removeAll(Collection c):移除一个集合的元素(移除一个以上返回的就是true) 删除的元素是两个集合的交集元素 
		如果没有交集元素 则删除失败 返回false
	c:判断功能
		boolean contains(Object o):判断集合中是否包含指定的元素	
		boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(这个集合 包含 另一个集合中所有的元素才算包含 才返回true)
		比如:1,2,3 containsAll 12=true   1,2,3 containsAll 2,3,4=false
		boolean isEmpty():判断集合是否为空
	d:获取功能
		Iterator<E> iterator()(重点)
	e:长度功能
			int size():元素的个数
	f:交集功能
			//例如:A集合对B集合取交集,获取到的交集元素在A集合中。返回的布尔值表示的是A集合是否发生变化 ,如果没有取到交集元素,那么A集合就会清空掉
			boolean retainAll(Collection c):获取两个集合的交集元素(交集:两个集合都有的元素)
	g:把集合转换为数组
			Object[] toArray()
案例1:
public class MyTest2 {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        collection.add(100);
        collection.add(200);
        collection.add(300);

        Collection collection1 = new ArrayList();
        collection1.add(400);
        collecti
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值