对象数组
需求:我有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接口的继承体系图
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单列集合体