java数组去重vector_18、集合(对象数组、Collection接口、List接口、Iterator、ListIterator、并发修改异常、ArrayList、Vector、LinkedLi...

对象数组

需求:我有3个学生,请把这个3个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。

学生:Student

成员变量:name,age

构造方法:无参,带参

成员方法:getXxx()/setXxx()

package org.westos.demo4;

/**

* @author lwj

* @date 2020/5/10 9:06

*/

public class Student {

private String name;

private Integer age;

public Student() {

}

public Student(String name, Integer age) {

this.name = name;

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

@Override

public String toString() {

return "Student{" +

"name='" + name + '\'' +

", age=" + age +

'}';

}

}

package org.westos.demo4;

/**

* @author lwj

* @date 2020/5/10 9:08

*/

public class MyTest {

public static void main(String[] args) {

Student[] students = new Student[3];

students[0] = new Student("张三", 21);

students[1] = new Student("李四", 22);

students[2] = new Student("王五", 23);

for (int i = 0; i < students.length; i++) {

System.out.println(students[i]);

}

//Student{name='张三', age=21}

//Student{name='李四', age=22}

//Student{name='王五', age=23}

}

}

Collection接口

数组和集合的区别:

长度区别

数组的长度是固定的

集合的长度是可变的

存储数据类型的区别

数组可以存储基本数据类型,也可以存储引用数据类型

集合只能存储引用数据类型

内容区别

数组只能存储同种数据类型的元素

集合可以存储不同类型的元素

Collection接口的继承体系图

d8d82ae7c57b8619d7ef2f80364f291e.png

Collection接口的功能概述

1、添加功能

boolean add(Object obj)

//添加一个元素

boolean addAll(Collection c)

//添加一个集合的元素

package org.westos.demo5;

import org.westos.demo4.Student;

import java.util.ArrayList;

import java.util.Collection;

/**

* @author lwj

* @date 2020/5/10 9:18

*/

public class MyTest {

public static void main(String[] args) {

Collection collection = new ArrayList();

collection.add("abc");

//String

collection.add(100);

//Integer

collection.add(new Integer(200));

collection.add(Integer.valueOf(300));

boolean b = collection.add(Double.valueOf(3.14));

System.out.println(b);

//true

collection.add(new Student("赵六", 31));

collection.add(true);

System.out.println(collection);

//[abc, 100, 200, 300, 3.14, Student{name='赵六', age=31}, true]

Collection collection1 = new ArrayList();

collection1.add(new Student("张三", 10));

collection1.add(new Student("李四", 20));

collection1.add(60);

collection.addAll(collection1);

System.out.println(collection);

//[abc, 100, 200, 300, 3.14, Student{name='赵六', age=31}, true, Student{name='张三', age=10}, Student{name='李四', age=20}, 60]

System.out.println(collection1);

//[Student{name='张三', age=10}, Student{name='李四', age=20}, 60]

}

}

2、删除功能

boolean remove(Object o)

//移除一个元素

boolean removeAll(Collection c)

//移除一个集合的元素(交集)

void clear()

//移除所有元素

package org.westos.demo5;

import java.util.ArrayList;

import java.util.Collection;

/**

* @author lwj

* @date 2020/5/10 9:40

*/

public class MyDemo {

public static void main(String[] args) {

Collection collection = new ArrayList();

collection.add("aaa");

collection.add("bbb");

collection.add("ccc");

collection.add("ddd");

collection.add("eee");

boolean eee = collection.remove("eee");

System.out.println(eee);

//true

System.out.println(collection);

//[aaa, bbb, ccc, ddd]

Collection collection1 = new ArrayList();

//按alt鼠标选则多行的collection变量

collection1.add("aaa");

collection1.add("bbb");

collection1.add("ccc");

boolean b = collection.removeAll(collection1);

System.out.println(b);

//true

System.out.println(collection);

//[ddd]

System.out.println(collection1);

//[aaa, bbb, ccc]

collection.clear();

//清空集合中的元素

System.out.println(collection);

//[]

collection1.clear();

System.out.println(collection1);

//[]

}

}

3、判断功能

boolean contains(Object o)

//判断集合中是否包含指定元素

boolean containsAll(Collection o)

//判断集合中是否包含指定集合的元素(这个集合包含另一个集合的所有元素才算包含,返回true)

boolean isEmpty()

//判断集合是否为空

package org.westos.demo5;

import java.util.ArrayList;

import java.util.Collection;

/**

* @author lwj

* @date 2020/5/10 10:11

*/

public class MyTest2 {

public static void main(String[] args) {

Collection collection = new ArrayList();

collection.add("aaa");

collection.add("bbb");

collection.add("ccc");

collection.add("ddd");

collection.add("eee");

boolean aaa = collection.contains("aaa");

System.out.println(aaa);

//true

Collection collection1 = new ArrayList();

collection1.add("aaa");

collection1.add("bbb");

collection1.add("ccc");

boolean b = collection.containsAll(collection1);

//collection1集合的所有元素都在collection集合中可以找到

System.out.println(b);

//true

System.out.println(collection.isEmpty());

//false

System.out.println(collection1.isEmpty());

//false

}

}

4、获取功能

Iterator iterator()

//获取集合的迭代器

int size()

//元素的个数

//Collection单列集合体

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值