Java10-集合ArrayList

1.ArrayList

集合和数组的优势对比:

  1. 长度可变
  2. 添加数据的时候不需要考虑索引,默认将数据添加到末尾

1.1 ArrayList类概述

  • 什么是集合

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

    长度可以变化,只能存储引用数据类型。
    
  • 泛型的使用

    用于*约束*集合中存储元素的*数据类型*
    

1.2 ArrayList类常用方法

1.2.1 构造方法

方法名 说明
public ArrayList() 创建一个空的集合对象

格式:ArrayList<E> array = new ArrayList<E>();

1.2.2 成员方法

方法名 说明
public boolean add(要添加的元素) 将指定的元素追加到此集合的末尾
public boolean remove(要删除的元素) 删除指定元素,返回值表示是否删除成功
public E remove(int index) 删除指定索引处的元素,返回被删除的元素
public E set(int index,E element) 修改指定索引处的元素,返回被修改的元素
public E get(int index) 返回指定索引处的元素
public int size() 返回集合中的元素的个数

1.2.3 示例代码

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
public class ArrayListDemo02 {
public static void main(String[] args) {
//创建集合
ArrayList<String> array = new ArrayList<String>();

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

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

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

//IndexOutOfBoundsException
array.remove(3); //IndexOutOfBoundsException

//public E set(int index,E element):修改指定索引处的元素,返回被修改的元素
array.set(1,"javaee"); //返回world。把1索引的值改成javaee

//IndexOutOfBoundsException
array.set(3,"javaee");// IndexOutOfBoundsException

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


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

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

1.3 ArrayList遍历

1
2
3
4
5
6
//遍历
//快捷键: list.fori 正向遍历
//list.forr 倒着遍历
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}

1.4 基本数据类型对应的包装类

byte Byte
shot Short
char Char
int Interger
long Long
float Float
double Double
boolean Boolean

1.5 添加对象

需求:

1,main方法中定义一个集合,存入三个用户对象。

用户属性为:id,username,password

2,要求:定义一个方法,根据id查找对应的学生信息。

如果存在,返回索引

如果不存在,返回-1

代码示例:

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
public class ArrayListDemo6 {
public static void main(String[] args) {
/*需求:
1,main方法中定义一个集合,存入三个用户对象。
用户属性为:id,username,password
2,要求:定义一个方法,根据id查找对应的学生信息。
如果存在,返回索引
如果不存在,返回-1*/


//1.创建集合对象
ArrayList<User> list = new ArrayList<>();

//2.创建用户对象
User u1 = new User("heima001", "zhangsan", "123456");
User u2 = new User("heima002", "lisi", "1234");
User u3 = new User("heima003", "wangwu", "1234qwer");

//3.把用户对象添加到集合当中
list.add(u1);
list.add(u2);
list.add(u3);

//4.调用方法,通过id获取对应的索引
int index = getIndex(list, "heima001");

System.out.println(index);

}


//1.我要干嘛? 根据id查找对应的学生信息
//2.我干这件事情需要什么才能完成? 集合 id
//3.方法的调用处是否需要继续使用方法的结果?
//要用必须返回,不要用可以返回也可以不返回
//明确说明需要有返回值 int
public static int getIndex(ArrayList<User> list, String id) {
//遍历集合得到每一个元素
for (int i = 0; i < list.size(); i++) {
User u = list.get(i);
String uid = u.getId();
if(uid.equals(id)){
return i;
}
}
//因为只有当集合里面所有的元素都比较完了,才能断定id是不存在的。
return -1;
}
}

1.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
public class ArrayListDemo5 {
public static void main(String[] args) {
/* 需求:
1,main方法中定义一个集合,存入三个用户对象。
用户属性为:id,username,password
2,要求:定义一个方法,根据id查找对应的学生信息。
如果存在,返回true
如果不存在,返回false*/

//1.定义集合
ArrayList<User> list = new ArrayList<>();

//2.创建对象
User u1 = new User("heima001","zhangsan","123456");
User u2 = new User("heima002","lisi","12345678");
User u3 = new User("heima003","wangwu","1234qwer");

//3.把用户对象添加到集合当中
list.add(u1);
list.add(u2);
list.add(u3);

//4.调用方法,查询id是否存在
boolean result = contains(list, "heima001");
System.out.println(result);

}

//定义在测试类中的方法需要加static
//1.我要干嘛? 我要根据id查询学生是否存在
//2.我干这件事情,需要什么才能完成? 集合 id
//3.方法的调用处是否需要使用方法的结果?
//如果要用,必须返回,如果不用,可以返回也可以不返回
//但是本题明确说明需要返回
public static boolean contains(ArrayList<User> list, String id){
//循环遍历集合,得到集合里面的每一个元素
//再进行判断

for (int i = 0; i < list.size(); i++) {
//i 索引 list.get(i); 元素
User u = list.get(i);
//判断id是否存在,我是拿着谁跟谁比较
//需要把用户对象里面的id拿出来再进行比较。
String uid = u.getId();
if(id.equals(uid)){
return true;//return 关键字:作用就是结束方法。
}
}
//只有当集合里面所有的元素全部比较完毕才能认为是不存在的。
return false;
}

}


Java10-集合ArrayList
https://blog.966677.xyz/2023/06/29/Java10-集合ArrayList/
作者
Zhou1317fe5
发布于
2023年6月29日
许可协议