Java - 集合

Java - 集合

1. ArrayList

1.1 ArrayList的成员方法

1
2
3
4
5
6
7
public boolean add(E e)                 // 将指定的元素添加到此集合的末尾
public void add(int index,E element) // 在此集合中的指定位置插入指定的元素
public E get(int index) // 返回指定索引处的元素
public int size() // 返回集合中的元素的个数
public E remove(int index) // 删除指定索引处的元素,返回被删除的元素
public boolean remove(Object o) // 删除指定的元素,返回删除是否成功
public E set(int index,E element) // 修改指定索引处的元素,返回被修改的元素

1.2 ArrayList的基本操作

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
public class ArrayListDemo01 {
public static void main(String[] args) {
// 集合增删改查基本操作
ArrayList<String> list = new ArrayList<>();

// 增加元素
boolean flag = list.add("one");
list.add("two");
list.add("three");
list.add("four");
list.add("five");
list.add("six");
System.out.println(flag); // true

// 删除元素
String removed = list.remove(0);
System.out.println(removed); // one

// 修改元素
list.set(2, "flag");
System.out.println(list); // [two,three,flag,five,six]

// 查询元素
String result = list.get(4);
System.out.println(result); // six

// 遍历集合
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}

2 案例

需求: 定义一个集合,添加字符串,并再进行遍历
遍历参照格式: [元素1,元素2,元素3]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class ArrayListTopic1 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
// 添加元素
list.add("1");
list.add("2");
list.add("3");
list.add("4");
StringJoiner joiner = new StringJoiner(",", "[", "]");
for (int i = 0; i < list.size(); i++) {
joiner.add(list.get(i));
}
System.out.println(joiner);
}
}

需求: 定义一个集合,添加整数,并再进行遍历
遍历参照格式: [元素1,元素2,元素3]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class ArrayListTopic2 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
// 添加元素
list.add(1);
list.add(2);
list.add(3);
list.add(4);
System.out.print("[");
for (int i = 0; i < list.size(); i++) {
if (i == list.size() - 1) {
System.out.print(list.get(i));
} else {
System.out.print(list.get(i) + ",");
}
}
System.out.print("]");
}
}

需求: 定义一个集合,添加一些学生对象再进行遍历,并再进行遍历
学生属性为:姓名、年龄

1
2
3
4
5
6
7
8
9
10
public class ArrayListTopic3 {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("张三", 17));
students.add(new Student("李四", 20));
for (int i = 0; i < students.size(); i++) {
System.out.println(students.get(i).getName() + " " + students.get(i).getAge());
}
}
}

需求: 定义一个集合,添加一些学生对象再进行遍历,并再进行遍历
学生属性为:姓名、年龄
数据来源通过键盘录入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class ArrayListTopic4 {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
Scanner sc = new Scanner(System.in);
int count = 1;
String name = "";
int age = 0;
while (true) {
System.out.println("输入名字");
name = sc.next();
System.out.println("输入年龄");
age = sc.nextInt();
students.add(new Student(name, age));
if (count == 3) {
break;
}
count++;
}

for (int i = 0; i < students.size(); i++) {
System.out.println(students.get(i).getName() + " " + students.get(i).getAge());
}
}
}

需求: 定义一个集合,添加一些用户对象
用户对象的属性为:id,username,password
要求:
定义一个方法,根据id查找对应的用户信息
如果存在 返回 true
不存在返回false

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
public class ArrayListTopic5 {
public static void main(String[] args) {
ArrayList<User> users = new ArrayList<>();
users.add(new User(1, "张三", "123456"));
users.add(new User(2, "李四", "123456"));
users.add(new User(3, "王五", "123456"));
users.add(new User(4, "赵六", "123456"));
boolean flag = selectById(users, 1);
if (flag) {
System.out.println("用户存在");
} else {
System.out.println("用户不存在");
}
}

public static boolean selectById(ArrayList<User> users, int id) {
for (int i = 0; i < users.size(); i++) {
if (users.get(i).getId().equals(id)) {
return true;
}
}
return false;
}

}

需求: 定义一个JavaBean类:Phone
手机对象的属性为:品牌 价格
main方法中定义一个集合,存入三个手机对象
分别为 小米 1000 苹果 7000 华为 2999
定义一个方法将价格低于3000的手机信息进行返回

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
public class ArrayListTopic6 {
public static void main(String[] args) {
ArrayList<Phone> phones = new ArrayList<>();
phones.add(new Phone("小米", 1000));
phones.add(new Phone("华为", 2999));
phones.add(new Phone("苹果", 7000));
ArrayList<Phone> result = verify(phones);
for (int i = 0; i < result.size(); i++) {
Integer price = result.get(i).getPrice();
String brand = result.get(i).getBrand();
System.out.println(brand + " " + price);
}
}

public static ArrayList<Phone> verify(ArrayList<Phone> list) {
ArrayList<Phone> phones = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getPrice() < 3000) {
phones.add(list.get(i));
}
}
return phones;
}

}

学生管理系统

需求:
采取控制台的方式去书写学生管理系统。
初始菜单:
"-------------欢迎来到学生管理系统----------------"
"1:添加学生"
"2:删除学生"
"3:修改学生"
"4:查询学生"
"5:退出"
"请输入您的选择:"
学生类:
  • 属性:id、姓名、年龄、家庭住址
添加功能:
  • 键盘录入每一个学生信息并添加,需要满足以下要求:
    id唯一
删除功能:
键盘录入要删除的学生id,需要满足以下要求:
  • id存在删除
  • id不存在,需要提示不存在,并回到初始菜单
修改功能:
键盘录入要修改的学生id,需要满足以下要求
  • id存在,继续录入其他信息
  • id不存在,需要提示不存在,并回到初始菜单
查询功能:
打印所有的学生信息,需要满足以下要求
  • 如果没有学生信息,提示:当前无学生信息,请添加后再查询
  • 如果有学生信息,需要按照以下格式输出。(不用过于纠结对齐的问题)
    id 姓名 年龄 家庭住址
    001 张三 23 南京

002 李四 24 北京
003 王五 25 广州
004 赵六 26 深圳

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
// 实体类
package cn.edu.wic.Demo3.pojo;

public class Student {
private Integer id;
private String name;
private Integer age;
private String address;

public Student() {
}

public Student(Integer id, String name, Integer age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

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;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}
}
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 实现类
package cn.edu.wic.Demo3;

import cn.edu.wic.Demo3.pojo.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class studentMangerMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Student> list = new ArrayList<>();
flag:
while (true) {
System.out.println("----------欢迎来到学生管理系统----------");
System.out.println("1:添加学生");
System.out.println("2:删除学生");
System.out.println("3:修改学生");
System.out.println("4:查询学生");
System.out.println("5:退出");
System.out.println("请输入您的选择:");
int choose = sc.nextInt();
if (choose > 0 && choose < 6) {
switch (choose) {
case 1:
add(list);
break;
case 2:
delete(list);
break;
case 3:
update(list);
break;
case 4:
queryStudent(list);
break;
case 5:
System.out.println("退出");
break flag;
default:
System.out.println("输入值有误");
break;
}
}
}
}

public static void add(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("输入用户ID");
int id = sc.nextInt();
if (!verify(list, id)) {
System.out.println("输入姓名");
String name = sc.next();
System.out.println("输入年龄");
int age = sc.nextInt();
System.out.println("输入地址");
String address = sc.next();
list.add(new Student(id, name, age, address));
System.out.println("添加成功");
break;
} else {
System.out.println("输入的ID重复,请重新输入");
}
}
}

public static void delete(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的id");
int id = sc.nextInt();
int index = getIndex(list, id);
if (index >= 0) {
list.remove(index);
System.out.println("id为" + id + "删除成功");
} else {
System.out.println("删除失败");
}

}

public static void update(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改的id");
int id = sc.nextInt();
int index = getIndex(list, id);
if (index >= 0) {
System.out.println("请输入修改后的姓名:");
String name = sc.next();
System.out.println("请输入修改后的年龄");
Integer age = sc.nextInt();
System.out.println("请输入修改的地址");
String address = sc.next();
list.set(index, new Student(id, name, age, address));
System.out.println("修改成功");
} else {
System.out.println("修改的ID不存在");
}
}

public static void queryStudent(ArrayList<Student> list) {
if (list.isEmpty()) {
System.out.println("当前暂无学生信息,请添加后在进行查询");
} else {
System.out.println("id\t姓名\t年龄\t家庭住址");
for (Student student : list) {
Integer id = student.getId();
String name = student.getName();
Integer age = student.getAge();
String address = student.getAddress();
System.out.println(id + "\t" + name + "\t" + age + "\t" + address);
}
}
}

public static boolean verify(ArrayList<Student> list, Integer id) {
return getIndex(list, id) >= 0;
}

public static int getIndex(ArrayList<Student> list, Integer id) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId().equals(id)) {
return i;
}
}
return -1;
}
}

学生管理系统升级版

需求:
为学生管理系统书写一个登陆、注册、忘记密码的功能。
只有用户登录成功之后,才能进入到学生管理系统中进行增删改查操作。
分析:
登录界面:
1
2
System.out.println("欢迎来到学生管理系统");
System.out.println("请选择操作1登录 2注册 3忘记密码");
用户类
属性:用户名、密码、身份证号码、手机号码
注册功能:

1.用户名需要满足以下要求:
验证要求:

  • 用户名唯一
  • 用户名长度必须在3~15位之间
  • 只能是字母加数字的组合,但是不能是纯数字

2.密码键盘输入两次,两次一致才可以进行注册。

3.身份证号码需要验证。
验证要求:

  • 长度为18位
  • 不能以0为开头
  • 前17位,必须都是数字
    -最为一位可以是数字,也可以是大写X或小写x

4,手机号验证。
验证要求:

  • 长度为11位
  • 不能以0为开头
  • 必须都是数字
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
// 实体类
package cn.edu.wic.Demo4.pojo;

public class User {
// 属性:用户名、密码、身份证号码、手机号码
private String username;
private String password;
private String id;
private String phone;

public User() {
}

public User(String username, String password, String id, String phone) {
this.username = username;
this.password = password;
this.id = id;
this.phone = phone;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", id='" + id + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// 实现类
package cn.edu.wic.Demo4;

import cn.edu.wic.Demo3.pojo.Student;
import cn.edu.wic.Demo4.pojo.User;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class StudentMangerUpdateSystem {
public static void main(String[] args) {
ArrayList<User> users = new ArrayList<>();
Scanner sc = new Scanner(System.in);
System.out.println("欢迎来到学生管理系统");
flag:
while (true) {
System.out.println("请选择操作1登录 2注册 3忘记密码");
int choose = sc.nextInt();
if (choose > 0 && choose < 4) {
switch (choose) {
case 1:
login(users);
ArrayList<Student> list = new ArrayList<>();
flag2:
while (true) {
System.out.println("----------欢迎来到学生管理系统----------");
System.out.println("1:添加学生");
System.out.println("2:删除学生");
System.out.println("3:修改学生");
System.out.println("4:查询学生");
System.out.println("5:退出");
System.out.println("请输入您的选择:");
int options = sc.nextInt();
if (options > 0 && options < 6) {
switch (options) {
case 1:
add(list);
break;
case 2:
delete(list);
break;
case 3:
update(list);
break;
case 4:
queryStudent(list);
break;
case 5:
System.out.println("退出");
break flag2;
default:
System.out.println("输入值有误");
break;
}
}
}
break;
case 2:
if (register(users)) {
System.out.println("创建成功");
}
break;
case 3:
forgotPassword(users);
break;
}
}
}

}

// 修改密码
public static boolean forgotPassword(ArrayList<User> users) {
Scanner sc = new Scanner(System.in);
String username = sc.next();
int index = getIndex(users, username);
if (index < 0) {
System.out.println("用户名不存在,请先注册");
} else {
while (true) {
System.out.println("请输入身份证号");
String id = sc.next();
System.out.println("请输入手机号");
String phone = sc.next();
if (forgotPasswordVerification(users, phone, id, index)) {
System.out.println("身份验证通过");
System.out.println("请输入修改的新密码");
String newPassword = sc.next();
users.get(index).setPassword(newPassword);
System.out.println("密码修改成功");
return true;
} else {
System.out.println("输入的身份证号或手机号有误 请重新输入");
}
}
}
return false;
}

// 登录业务
public static boolean login(ArrayList<User> users) {
Scanner sc = new Scanner(System.in);
String username = sc.next();
int index = getIndex(users, username);
boolean flag = false;
if (index < 0) {
System.out.println("用户名不存在,请先注册");
} else {
while (true) {
String captcha = captchaGeneration();
System.out.println("您当前的验证码为:" + captcha);
System.out.println("请输入密码:");
String password = sc.next();
System.out.println("请输入验证码:");
String captchaConfirm = sc.next();
if (passwordVerification(users, password, index) && captcha.equals(captchaConfirm)) {
System.out.println("登录成功");
flag = true;
break;
}
}
}
return flag;
}

// 注册业务
public static boolean register(ArrayList<User> users) {
Scanner sc = new Scanner(System.in);
String username;
String password;
String repeatPassword;
String resultPassword;
String phone;
String id;
while (true) {
System.out.println("请输入用户名:");
username = sc.next();
if (usernameValidation(users, username)) {
System.out.println("用户名输入无误");
break;
} else {
System.out.println("用户名有误,请重新输入:");
}
}

while (true) {
System.out.println("请输入密码");
password = sc.next();
System.out.println("请再次输入密码");
repeatPassword = sc.next();
if (passwordDuplicateConfirmation(password, repeatPassword)) {
resultPassword = password;
System.out.println("密码输入无误");
break;
} else {
System.out.println("两次密码输入不一致,请重新输入");
}
}

while (true) {
System.out.println("请输入身份证号");
id = sc.next();
if (idValidation(id)) {
System.out.println("身份证输入无误");
break;
} else {
System.out.println("身份证输入错误");
}
}

while (true) {
System.out.println("请输入手机号");
phone = sc.next();
if (phoneValidation(phone)) {
System.out.println("手机号输入无误");
break;
} else {
System.out.println("请重新输入手机号");
}
}
return users.add(new User(username, resultPassword, id, phone));
}

// 判断身份证号和手机号是否与用户名一致
public static boolean forgotPasswordVerification(ArrayList<User> users, String phone, String id, int index) {
if (users.get(index).getPhone().equals(phone) && users.get(index).getId().equals(id)) {
return true;
}
return false;
}

// 验证密码是否和索引里的密码一致
public static boolean passwordVerification(ArrayList<User> users, String password, int index) {
return users.get(index).getPassword().equals(password);
}

// 验证手机号是否合法
public static boolean phoneValidation(String phoneNumber) {
if (phoneNumber.length() == 11) {
for (int i = 0; i < phoneNumber.length(); i++) {
char c = phoneNumber.charAt(i);
if (c >= '0' && c <= '9') {
return true;
}
}
} else {
return false;
}
return false;
}

// 验证身份证是否合法
public static boolean idValidation(String id) {
if (id.length() == 18) {

} else {
return false;
}

if (id.charAt(0) != '0') {

} else {
return false;
}

for (int i = 0; i < id.length() - 1; i++) {
char c = id.charAt(i);
if (c >= '0' && c <= '9') {
char ch = id.charAt(id.length() - 1);
if (ch >= '0' && ch <= '9' || ch == 'x' || ch == 'X') {
return true;
}
} else {
return false;
}
}
return false;
}

// 判断密码是否一致
public static boolean passwordDuplicateConfirmation(String str1, String str2) {
return str1.equals(str2);
}

// 判断用户名是否符合校验条件
public static boolean usernameValidation(ArrayList<User> users, String username) {
int charCount = 0;
int numberCount = 0;
int index = getIndex(users, username);
if (index == -1) {
if (username.length() >= 3 && username.length() < 16) {
for (int i = 0; i < username.length(); i++) {
char c = username.charAt(i);
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
charCount++;
} else if (c >= '0' && c <= '9') {
numberCount++;
} else {
return false;
}
}
}
if (charCount > 0 && numberCount > 0 && numberCount < username.length()) {
return true;
}
} else {
return false;
}
return false;
}

// 判断用户名是否存在
public static int getIndex(ArrayList<User> users, String str) {
for (int i = 0; i < users.size(); i++) {
if (users.get(i).getUsername().equals(str)) {
return i;
}
}
return -1;
}

// 验证码生成
public static String captchaGeneration() {
char[] arr = new char[52];
for (int i = 0; i < 26; i++) {
arr[i] = (char) ('a' + i);
}

int temp = 0;
for (int i = 26; i < arr.length; i++) {
arr[i] = (char) ('A' + temp);
temp++;
}

StringBuilder builder = new StringBuilder();
Random r = new Random();
int randomNumber = 0;
int i = 0;
while (i < 4) {
randomNumber = r.nextInt(arr.length);
builder.append(arr[randomNumber]);
i++;
}
builder.append(r.nextInt(10));

char temporary;
String value = builder.toString();
char[] array = value.toCharArray();
randomNumber = r.nextInt(array.length - 1);
temporary = array[randomNumber];
array[randomNumber] = array[array.length - 1];
array[array.length - 1] = temporary;
String result = new String(array);
return result;
}


public static void add(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("输入用户ID");
int id = sc.nextInt();
if (!verify(list, id)) {
System.out.println("输入姓名");
String name = sc.next();
System.out.println("输入年龄");
int age = sc.nextInt();
System.out.println("输入地址");
String address = sc.next();
list.add(new Student(id, name, age, address));
System.out.println("添加成功");
break;
} else {
System.out.println("输入的ID重复,请重新输入");
}
}
}

public static void delete(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的id");
int id = sc.nextInt();
int index = getIndex(list, id);
if (index >= 0) {
list.remove(index);
System.out.println("id为" + id + "删除成功");
} else {
System.out.println("删除失败");
}

}

public static void update(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改的id");
int id = sc.nextInt();
int index = getIndex(list, id);
if (index >= 0) {
System.out.println("请输入修改后的姓名:");
String name = sc.next();
System.out.println("请输入修改后的年龄");
Integer age = sc.nextInt();
System.out.println("请输入修改的地址");
String address = sc.next();
list.set(index, new Student(id, name, age, address));
System.out.println("修改成功");
} else {
System.out.println("修改的ID不存在");
}
}

public static void queryStudent(ArrayList<Student> list) {
if (list.isEmpty()) {
System.out.println("当前暂无学生信息,请添加后在进行查询");
} else {
System.out.println("id\t姓名\t年龄\t家庭住址");
for (Student student : list) {
Integer id = student.getId();
String name = student.getName();
Integer age = student.getAge();
String address = student.getAddress();
System.out.println(id + "\t" + name + "\t" + age + "\t" + address);
}
}
}

public static boolean verify(ArrayList<Student> list, Integer id) {
return getIndex(list, id) >= 0;
}

public static int getIndex(ArrayList<Student> list, Integer id) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId().equals(id)) {
return i;
}
}
return -1;
}

}