集合概述

编程的时候如果要存储多个数据,使用长度固定的数组存储格式,不一定能满足我们的需求,更适应不了变化的需求

集合类的特点:提供一种存储空间可变的存储模型,存储的数据量可以发生改变

集合类有很多,目前先学习:ArrayList

ArrayList:

  • 可调整大小的数组实现
  • :是一种特殊的数据类型,范型

怎么用?

  • 在出现E的地方我们使用引用数据类型替换即可
  • 举例:ArrayList,ArrayList

ArrayList构造方法和添加方法

方法名 说明
public ArrayList() 创建一个空的集合对象
public boolean add(E e) 将指定的元素追加到此集合的末尾
public void add(int index,E element) 在此集合中的指定位置插入指定的元素
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.ArrayList;

/*
ArrayList构造方法:
public ArrayList ():创建一个空的集合对象
ArrayList添加方法
public boolean add(E e):将指定的元素追加到此集合的末尾
public void add(int index,E element):在此集合中指定位置插入指定的元素
*/
public class ArrayListDemo01 {
public static void main(String[] args) {
// public ArrayList ():创建一个空的集合对象
//ArrayList<String> array = new ArrayList<>(); jdk7以后新特性
ArrayList<String> array = new ArrayList<String>();

//public boolean add(E e):将指定的元素追加到此集合的末尾
//System.out.println(array.add("hello"));

array.add("hello");
array.add("grape");

//public void add(int index,E element):在此集合中指定位置插入指定的元素
array.add(1,"sesh"); //true
array.add(2,"sesh"); //true
array.add(3,"sesh"); //索引越界

//输出集合
System.out.println("array:"+array);
}
}

ArrayList集合常用方法

方法名 说明
public boolean remove(Object o) 删除指定的元素,返回删除是否成功
public E remove(int index) 删除指定索引处的元素,返回被删除的元素
public E set(int index,E element) 修改指定索引处的元素,返回被修改的元素
public E get(int index) 返回指定索引处的元素
public int size() 返回集合中的元素的个数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.ArrayList;

/*
ArrayList集合常用方法
public boolean remove(Object o):删除指定的元素,返回删除是否成功
public E remove(int index):删除指定索引处的元素,返回被删除的元素
public E set(int index,E element):修改指定索引处的元素,返回被修改的元素
public E get(int index):返回指定索引处的元素
public int size():返回集合中的元素的个数
* */
public class ArrayListDemo02 {
public static void main(String[] args) {
//创建集合
ArrayList<String> array = new ArrayList<String>();

//添加元素
array.add("hello");
array.add("grape");
array.add("java");

//public boolean remove(Object o):删除指定的元素,返回删除是否成功
//System.out.println(array.remove("grape"));
//System.out.println(array.remove("javaee"));

//public E remove(int index):删除指定索引处的元素,返回被删除的元素
//System.out.println(array.remove(1));

//public E set(int index,E element):修改指定索引处的元素,返回被修改的元素
//System.out.println(array.set(1,"apple"));
//System.out.println(array.set(2,"banana"));

//public E get(int index):返回指定索引处的元素
//System.out.println(array.get(2));

//public int size():返回集合中的元素的个数
System.out.println(array.size());


//输出集合
System.out.println("array:"+array);
}
}

案例:存储字符串并遍历

需求:创建一个存储字符串的集合,存储3个字符串元素,使用程序实现在控制台遍历该集合

思路:

  1. 创建集合对象

  2. 往集合中添加字符串对象

  3. 遍历集合,首先要能够获取到集合中的每一个元素,通过get(int index)方法实现

  4. 遍历集合,其次要能够获取到集合的长度,这个通过size()方法实现

  5. 遍历集合的通用合适

    1
    2
    3
    for(int i=0;i<集合对象.size();i++){
    集合对象.get(i) 就是指索引处的元素
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.ArrayList;

public class ArrayListDemo03 {
public static void main(String[] args) {
//创建集合对象
ArrayList<String> array = new ArrayList<String>();

//在集合对象中添加字符串对象
array.add("任天堂");
array.add("索尼");
array.add("微软");
array.add("世嘉");

//遍历集合,首先要能够获取到集合中的每一个元素,这个通过get(int index)
/*System.out.println(array.get(0));
System.out.println(array.get(1));
System.out.println(array.get(2));
System.out.println(array.get(3));*/

/*for (int i=0;i< 3;i++){
System.out.println(array.get(i));
}*/

for (int i = 0; i < array.size(); i++) {
String s = array.get(i);
System.out.println(s);
}
}
}

案例:存储学生对象并遍历

需求: 创建一个存储学生对象的集合,存储3个学生对象,使用程序实现控制台遍历该集合

思路:

  1. 定义学生类

  2. 创建集合对象

  3. 创建学生对象

  4. 添加学生对象到集合中

  5. 遍历集合,采用通用遍历格式实现

学生类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
* 学生类
* */
public class Student {
private String name;
private int attack;

public Student(){}

public Student(String name, int attack){
this.name = name;
this.attack = attack;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAttack() {
return attack;
}

public void setAttack(int attack) {
this.attack = attack;
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.ArrayList;

public class StudentDemo {
public static void main(String[] args) {
//创建集合对象
ArrayList<Student> array = new ArrayList<Student>();

//创建学生对象
Student s1 = new Student("任天堂",2000);
Student s2 = new Student("索尼",1700);
Student s3 = new Student("微软",1500);
Student s4 = new Student("世嘉",5);

//添加学生对象到集合中
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);

//遍历集合,采用通用遍历格式实现
for (int i =0;i< array.size();i++){
Student s = array.get(i);
System.out.println(s.getName()+","+s.getAttack());

}
}
}

案例:存储学生对象并遍历,升级版

需求: 创建一个存储学生对象的集合,存储3个学生对象,使用程序实现控制台遍历该集合学生的姓名和年龄来自于键盘录入

思路:

  1. 定义学生类,为了键盘录入数据方便,把学生类中的成员变量都定义为String类型

  2. 创建集合对象

  3. 键盘录入学生对象所需的数据

  4. 创建学生对象,把键盘录入的数据赋值给学生对象的成员变量

  5. 往集合中添加学生变量

  6. 遍历集合,采用通用格式遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.ArrayList;
import java.util.Scanner;

public class Demo {
public static void main(String[] args) {
//创建集合对象
ArrayList<Student> array = new ArrayList<Student>();

/*
//键盘录入学生对象所需要的数据
Scanner sc = new Scanner(System.in);

System.out.println("请输入学生姓名");
String name = sc.nextLine();

System.out.println("请输入学生年龄");
String age = sc.nextLine();

//创建学生对象,把键盘录入的数据赋值给学生对象的成员
Student s = new Student();
s.setName(name);
s.setAge(age);

//往集合中添加学生对象
array.add(s);
*/

//为了提高代码的复用性,我们用方法来改进程序

addStudent(array);
addStudent(array);
addStudent(array);

//遍历集合,采用通用遍历格式实现
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
System.out.println(s.getName() + "," + s.getAge());
}
}
/*
两个明确:
返回值类型:void
参数:ArrayList<Student> array
*/
public static void addStudent(ArrayList<Student> array){
//键盘录入学生对象所需要的数据
Scanner sc = new Scanner(System.in);

System.out.println("请输入学生姓名");
String name = sc.nextLine();

System.out.println("请输入学生年龄");
String age = sc.nextLine();

//创建学生对象,把键盘录入的数据赋值给学生对象的成员
Student s = new Student();
s.setName(name);
s.setAge(age);

//往集合中添加学生对象
array.add(s);
}
}