-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaggregation6.java
More file actions
72 lines (58 loc) · 1.87 KB
/
aggregation6.java
File metadata and controls
72 lines (58 loc) · 1.87 KB
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
/*
* @由于个人水平有限, 难免有些错误, 还请指点:
* @Author: cpu_code
* @Date: 2020-09-16 20:59:35
* @LastEditTime: 2020-09-16 20:59:59
* @FilePath: \java\aggregation\aggregation6.java
* @Gitee: [https://gitee.com/cpu_code](https://gitee.com/cpu_code)
* @Github: [https://github.com/CPU-Code](https://github.com/CPU-Code)
* @CSDN: [https://blog.csdn.net/qq_44226094](https://blog.csdn.net/qq_44226094)
* @Gitbook: [https://923992029.gitbook.io/cpucode/](https://923992029.gitbook.io/cpucode/)
*/
package aggregation;
import java.util.ArrayList;
import java.util.List;
public class aggregation6 {
public static void main(String[] args) {
// 创建List集合对象
List<String> list = new ArrayList<String>();
// 往 尾部添加 指定元素
list.add("cpucode");
list.add("cpu");
System.out.println(list);
// add(int index,String s) 往指定位置添加
list.add(1, "笨");
System.out.println(list);
// String remove(int index) 删除指定位置元素 返回被删除元素
// 删除索引位置为2的元素
System.out.println("删除第二个");
System.out.println(list.remove(2));
System.out.println(list);
// String set(int index,String s)
// 在指定位置 进行 元素替代(改)
// 修改指定位置元素
list.set(0, "hello");
System.out.println(list);
// String get(int index) 获取指定位置元素
// 跟size() 方法一起用 来 遍历的
for (int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
}
//还可以使用增强for
for (String string : list){
System.out.println(string);
}
}
}
/*
[cpucode, cpu]
[cpucode, 笨, cpu]
删除第二个
cpu
[cpucode, 笨]
[hello, 笨]
hello
笨
hello
笨
*/