JAVA集合
对象数组
集合类之ArrayList
学生管理系统
斗地主案例
NO.one 对象数组
1.1 对象数组描述
A:基本类型的数组:存储的元素为基本类型
int[] arr={1,2,3,4}
B:对象数组:存储的元素为引用类型
Student[] stus=new Student[3];
Student代表一个自定义类
Stus数组中stus[0],stus[1],stus[2]的元素数据类型为Student,
都可以指向一个Student对象
1.2 对象数组案例:
创建一个学生数组,存储三个学生对象并遍历
1.2.1 案例代码一:
packagecom.gao;/** 自动生成构造方法:
* 代码区域右键 -- Source -- Generate Constructors from Superclass... 无参构造方法
* 代码区域右键 -- Source -- Generate Constructor using Fields... 带参构造方法
* 自动生成getXxx()/setXxx():
* 代码区域右键 -- Source -- Generate Getters and Setters...*/
public classStudent {privateString name;private intage;publicStudent() {
}public Student(String name, intage) {this.name =name;this.age =age;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}
}packagecom.itheima;/** 创建一个学生数组,存储三个学生对象并遍历
*
* 分析:
* A:定义学生类
* B:创建学生数组
* C:创建学生对象
* D:把学生对象作为元素赋值给学生数组
* E:遍历学生数组*/
public classStudentDemo {public static voidmain(String[] args) {//创建学生数组
Student[] students = new Student[3];//创建学生对象
Student s1 = new Student("曹操",40);
Student s2= new Student("刘备",35);
Student s3= new Student("孙权",30);//把学生对象作为元素赋值给学生数组
students[0] =s1;
students[1] =s2;
students[2] =s3;//遍历学生数组
for(int x=0; x
Student s=students[x];//System.out.println(s);
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
1.3 对象数组的内存图
NO.two 集合类ArrayList
2.1 集合概述
A:我们学习的是面向对象编程语言,而面向对象编程语言对事物的描述都是通过对象来体现的。
为了方便对多个对象进行操作,我们就必须对这多个对象进行存储,而要想对多个对象进行存储,
就不能是一个基本 的变量,而应该是一个容器类型的变量。
B:StringBuilder,数组。
StringBuilder的结果只能是一个字符串类型,不一定满足我们的需求。
所以,我们目前只能选择数组了,也就是我们前面学习过的对象数组。
但是,数组的长度是固定的, 如果有时候元素的个数不确定的,我们无法定义出数组的长度,这个时候,
java就提供了集合类供我们使用。
2.2 ArrayList集合
2.2.1 ArrayList 添加新元素
2.2.1.1 案例代码二:
packagecom.gao_01;importjava.util.ArrayList;/** 为什么会出现集合类:
* 我们学习的是面向对象编程语言,而面向对象编程语言对事物的描述都是通过对象来体现的。
* 为了方便对多个对象进行操作,我们就必须对这多个对象进行存储,而要想对多个对象进行存储,
* 就不能是一个基本的变量,而应该是一个容器类型的变量。
* 到目前为止,我们学习过了哪些容器类型的数据呢?StringBuilder,数组。
* StringBuilder的结果只能是一个字符串类型,不一定满足我们的需求。
* 所以,我们目前只能选择数组了,也就是我们前面学习过的对象数组。
* 但是,数组的长度是固定的,适应不了变化的需求,那么,我们该如何选择呢?
* 这个时候,java就提供了集合类供我们使用。
*
* 集合类的特点:
* 长度可变。
*
* ArrayList:
* 大小可变数组的实现
*
* :是一种特殊的数据类型,泛型。
* 怎么用呢?
* 在出现E的地方我们使用引用数据类型替换即可
* 举例:ArrayList,ArrayList
*
* 构造方法:
* ArrayList()
*
* 添加元素:
* public boolean add(E e):添加元素
* public void add(int index,E element):在指定的索引处添加一个元素*/
public classArrayListDemo {public static voidmain(String[] args) {//创建集合对象
ArrayList array = new ArrayList();//add(E e):添加元素
array.add("hello");
array.add("world");
array.add("java");//add(int index,E element):在指定的索引处添加一个元素//array.add(1, "android");
System.out.println("array:"+array);
}
}
2.2.2 删改查方法
A:获取元素
public E get(int index):返回指定索引处的元素
B:集合长度
public int size():返回集合中的元素的个数
C:删除元素
public boolean remove(Object o):删除指定的元素,返回删除是否成功
public E remove(int index):删除指定索引处的元素,返回被删除的元素
D:修改元素
public E set(int index,E element):修改指定索引处的元素,返回被修改的元素
2.2.2.1 案例代码三:
packagecom.gao_01;importjava.util.ArrayList;/** 获取元素
* public E get(int index):返回指定索引处的元素
* 集合长度
* public int size():返回集合中的元素的个数
* 删除元素
* public boolean remove(Object o):删除指定的元素,返回删除是否成功
* public E remove(int index):删除指定索引处的元素,返回被删除的元素
* 修改元素
* public E set(int index,E element):修改指定索引处的元素,返回被修改的元素*/
public classArrayListDemo2 {public static voidmain(String[] args) {//创建集合对象
ArrayList array = new ArrayList();//添加元素
array.add("hello");
array.add("world");
array.add("java");//public E get(int index):返回指定索引处的元素//System.out.println("get:"+array.get(0));//System.out.println("get:"+array.get(1));//System.out.println("get:"+array.get(2));//public int size():返回集合中的元素的个数//System.out.println("size:"+array.size());//public boolean remove(Object o):删除指定的元素,返回删除是否成功//System.out.println("remove:"+array.remove("world"));//true//System.out.println("remove:"+array.remove("world"));//false//public E remove(int index):删除指定索引处的元素,返回被删除的元素//System.out.println("remove:"+array.remove(0));//public E set(int index,E element):修改指定索引处的元素,返回被修改的元素
System.out.println("set:"+array.set(1, "android"));//输出
System.out.println("array:"+array);
}
}
2.2.3 ArrayList遍历
集合的遍历思想和数组的遍历思想相同
循环遍历容器,依次取出里面的元素即可
2.2.3.1 案例代码四:
packagecom.gao_01;importjava.util.ArrayList;/** ArrayList集合的遍历
* 通过size()和get()配合实现的*/
public classArrayListDemo3 {public static voidmain(String[] args) {//创建集合对象
ArrayList array = new ArrayList();//添加元素
array.add("hello");
array.add("world");
array.add("java");//获取元素//原始做法
System.out.println(array.get(0));
System.out.println(arra