-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTestList.java
executable file
·59 lines (55 loc) · 1.69 KB
/
TestList.java
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
import java.util.ArrayList;
import java.util.List;
/** òåñòèðîâàíèå èçìåíåíèÿ List â ñëó÷àå ïåðåäà÷è ýòîãî List â ìåòîä */
public class TestList {
public static void main(String[] args){
ArrayList<String> list=new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
list.add("four");
list.add("one");
list.add("two");
list.add("three");
list.add("one");
list.add("two");
list.add("three");
System.out.println("List before:"+list);
editList(list);
System.out.println("List after:"+list);
// show method "indexOf" and "lastIndexOf"
// has not allowed:
/*
for(String value:list){
if(list.indexOf(value)!=list.lastIndexOf(value)){
int firstIndex=list.indexOf(value);
int lastIndex=list.lastIndexOf(value);
System.out.println("REMOVE: "+value+": indexOf:"+firstIndex+" lastIndexOf:"+lastIndex);
list.remove(lastIndex);
}else{
System.out.println(value+": indexOf:"+list.indexOf(value)+" lastIndexOf:"+list.lastIndexOf(value));
}
}*/
ArrayList<String> listWithNotRepeated=new ArrayList<String>();
for(int counter=0;counter<list.size();counter++){
String currentValue=list.get(counter);
int firstIndex=list.indexOf(currentValue);
int lastIndex=list.lastIndexOf(currentValue);
if(firstIndex!=lastIndex){
if(counter==firstIndex){
listWithNotRepeated.add(currentValue);
}
}else{
// only one value
listWithNotRepeated.add(currentValue);
}
}
System.out.println("After process:"+listWithNotRepeated);
}
private static void editList(List<String> list){
if(list!=null){
list.add("new value");
list.remove(0);
}
}
}