一、集合的引入
在没有接触集合之前
需求:我有5个学生,5个学生有自己的姓名,年龄,遍历当前学生数组,获取到每一个学生的信息
创建对象数组:可以存储对象的数组
1)自定义类:Student name,age
2)在测试类中:创建一个数组,可以存储Stduent类型的
3)根据的提供长度,分别创建5个具体的学生对象
4)赋值并且遍历
public class Student {
private String name ;
private int age ;
public Student() {
super();
}
public Student(String name, int age) {
super();
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 ObjectArrayDemo {
public static void main(String[] args) {
//创建一个数组,可以存储Student类型 (对象数组)
//创建数组的格式:int[] arr= new int[长度] ;
Student[] students = new Student[5] ;
//需要创建5个学生对象
Student s1 = new Student("高圆圆", 27) ;
Student s2 = new Student("王力宏", 30) ;
Student s3 = new Student("唐嫣", 29) ;
Student s4 = new Student("克劳泽",35) ;
Student s5 = new Student("拉姆", 36) ;
//赋值
students[0] = s1 ;
students[1] = s2 ;
students[2] = s3 ;
students[3] = s4 ;
students[4] = s5 ;
//遍历学生数组
for(int x = 0 ; x < students.length ; x ++) {
Student s = students[x] ;
System.out.println(s.getName()+"----"+s.getAge());
}
}
}
集合的由来?
学生的面向对象语言,面向对象语言对事物的描述是通过对象体现的,那么需求需要来存储多个对象.
要存储多个对象,不能使用基本类型的变量,需要使用容器类型的变量? 之前学习过的容器变量:数组 ,字符串缓冲区(StringBuffer)
对于字符串缓冲区来说,在内存中始终存储的是字符串,不能满足要求;数组呢,数组的长度是固定的,不符合长度变化的要求,所以Java提供了一个Collection 集合。
首先来看一到面试题:数组和集合的区别?
1)长度区别:
数组长度固定
集合长度可变
2)内容的区别
数组可以存储同一种类型的元素
集合可以存储多种类型的元素
3)存储类型的区别
数组:可以存储基本类型,也可以存储引用类型 String[] str = {"hello","world","java",100} ; 错误的
集合:只能存储引用类型
集合Collection:子接口有两个,两个子接口分别对应多个子实现类,多个集合数据结构不同,但是他们有共性内容,将共性内容抽取出来,就可以集合继承体系图!
数据结构:数据的存储方式
Collection:
Collection 层次结构 中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的,这个我们后面会细说。
JDK 不提供此接口的任何直接 实现:它提供更具体的子接口,更具体的实现类
基本功能:
添加功能:
boolean add(Object e)
删除功能:
void clear() :删除集合中所有元素(暴力删除)
boolean remove(Object o):删除集合中的指定元素
判断功能:
boolean contains(Object o):集合中是否包含指定的元素
获取功能:
int size() :获取集合中的元素数
转换功能:
Object[] toArray() :将集合转换成数组
public class CollectionDemo {
public static void main(String[] args) {
//创建集合对象
//Collection c = new Collecton() ; //不能实例化
Collection c = new ArrayList() ;
System.out.println(c);
c.add("hello") ;
c.add("world") ;
c.add("java") ;
System.out.println("c:"+c) ;
//删除功能:c.clear();
System.out.println("remove():"+c.remove("java"));
System.out.println("c:"+c);
//boolean contains(Object o):集合中是否包含指定的元素
System.out.println("contains():"+c.contains("android"));
System.out.println("contains():"+c.contains("hello"));
System.out.println(c.size());
//boolean isEmpty() :判断集合是否为空
System.out.println(c.isEmpty());
}
}
Colleciton的集合的高级功能:
boolean addAll(Collection c) :添加一个集合中的所有元素boolean removeAll(Collection c):删除一个算是删除
boolean containsAll(Collection c):包含所有元素算是包含
boolean retainAll(Collection c):A集合对B集合取交集,交集的元素要去A集合中,boolean返回值表达的A集合的元素是否发生变化,如果发生变化,则返回true,否则,返回false
public class CollectionDemo {
public static void main(String[] args) {
//创建Collection集合1
Collection c1 = new ArrayList() ;
//添加元素
c1.add("abc1") ;
c1.add("abc2") ;
c1.add("abc3") ;
c1.add("abc4") ;
//创建第二个集合
Collection c2 = new ArrayList() ;
c2.add("abc1") ;
c2.add("abc2") ;
c2.add("abc3") ;
c2.add("abc4") ;
// c2.add("abc5") ;
// c2.add("abc6") ;
// c2.add("abc7") ;
// System.out.println(c1.addAll(c2));
// System.out.println(c1.removeAll(c2));
// System.out.println(c1.containsAll(c2));
System.out.println("retainAll():"+c1.retainAll(c2));
System.out.println("c1:"+c1);
System.out.println("c2:"+c2);
}
}
这个例子程序不太好静态展示出来,大家可以下去自己试试。
toArray方法的练习:
public class CollectionDemo2 {
public static void main(String[] args) {
//创建一个集合对象
Collection c = new ArrayList() ;
//给集合中添加元素
c.add("hello") ;
c.add("world") ;
c.add("java") ;
c.add("JavaEE") ;
//需要去转换
//Object[] toArray()
//将集合转成数组
Object[] objs = c.toArray() ;
for(int x =0 ; x < objs.length ; x ++) {
//System.out.println(objs[x]);
//需求:输出不同的元素的同时,还需要输出字符串元素的长度
//System.out.println(objs[x]+"----"+objs[x].length());
/**
*上面代码有问题
* length():属于String类型的特有功能,可以获取字符串长度
*/
String str = (String) objs[x] ; //相当于:向下转型
System.out.println(str+"----"+str.length());
}
}
}
重新用Collection改进一下刚才用对象数组写的那个Student程序:(Student还和之前的一样,这里就不再写了)
public class CollectionDemo {
public static void main(String[] args) {
//创建一个集合对象
Collection c = new ArrayList() ;
//创建5个具体学生对象
Student s1 = new Student("高圆圆", 27) ;
Student s2 = new Student("杨桃", 28) ;
Student s3 = new Student("王力宏", 35) ;
Student s4 = new Student("周星驰", 60) ;
Student s5 = new Student("成龙", 55) ;
//添加到集合中
c.add(s1) ;
c.add(s2) ;
c.add(s3) ;
c.add(s4) ;
c.add(s5) ;
//转换数组
Object[] objs = c.toArray() ;
//遍历
for(int x = 0 ; x < objs.length ;x ++) {
// System.out.println(objs[x]);
//需求:需要的通过getXXX方法获取学生信息
Student s = (Student) objs[x] ; //向下转型
System.out.println(s.getName()+"----"+s.getAge());
}
}
}
集合剩下的部分我们下次再讲~