-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArrayList
More file actions
59 lines (43 loc) · 1.85 KB
/
Copy pathArrayList
File metadata and controls
59 lines (43 loc) · 1.85 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
ArrayList属于List集合的子类, 具有:有序(存,取顺序一致),有索引,可重复存储的特点.
要求:
用ArrayList去除字符串中重复的值
分析:
1.创建新集合
2.将新集合中的值与老集合中的值进行对比,若老集合中的值新集合中没有,则新集合对该老集合的值进行存储
代码:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class RemoveTheSameData_ArrayList { //ArrayList去重字符串中重复的值
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Object> l = new ArrayList<>();
System.out.println("please input a ArrayList:");
while (sc.hasNext()) {
l.add(sc.nextLine()); //ctrl + z ==> 停止输入
}
/*l.add("a");
l.add("b");
l.add("e");
l.add("b");
l.add("a");
l.add("a");
l.add("c");
l.add("a");*/
ArrayList<Object> newList = getSingle(l);
System.out.println("\nthe new ArrayList is:");
System.out.println(newList);
sc.close();
}
public static ArrayList<Object> getSingle(ArrayList<?> list) {
ArrayList<Object> newList = new ArrayList<>(); //建立新集合
Iterator<?> it = list.iterator(); //获取老集合的迭代器
while (it.hasNext()) {
Object obj = it.next();
if (!newList.contains(obj)) {
newList.add(obj);
}
}
return newList;
}
}