forked from igortereshchenko/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkWithList.java
More file actions
128 lines (83 loc) · 2.77 KB
/
WorkWithList.java
File metadata and controls
128 lines (83 loc) · 2.77 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
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
package LessonCollection;
import java.util.ArrayList;
import java.util.List;
/**
* Created by student on 21.03.2018.
*/
public class WorkWithList {
public static void main(String[] args) {
//List
List<String> stringList = new ArrayList<String>();
stringList.add("Germany");
stringList.add("Ukraine");
stringList.add("Greate Britain");
stringList.add("Spain");
stringList.add("Italy");
for (String item:stringList
) {
System.out.println(item);
}
stringList.add(1,"New country");
System.out.println("\n added new country \n");
for (String item:stringList
) {
System.out.println(item);
}
System.out.println("\n change new country on Poland\n");
stringList.set(1,"Poland");
for (String item:stringList
) {
System.out.println(item);
}
System.out.println("\n remove Greate Britain\n");
stringList.remove("Greate Britain");
for (String item:stringList
) {
System.out.println(item);
}
System.out.println("\n convert to array \n");
Object[] objects = stringList.toArray();
for (int i = 0; i < objects.length; i++) {
System.out.println(objects[i]);
}
String[] strings = stringList.toArray(new String[stringList.size()]);
String[] arrayToConver = new String[stringList.size()];
String[] strings2 = stringList.toArray(arrayToConver);
for (String item: strings
) {
System.out.println(item);
}
stringList.clear();
System.out.println("\n cleared list \n");
for (String item: stringList
) {
System.out.println(item);
}
//ArrayList<int> ints = new ArrayList<>(int); //int double float long ... => Ineteger Double Float Long
int[] ints = {1,2,3,4};
ArrayList<Integer> integerArrayList = new ArrayList<Integer>();
integerArrayList.add(1);
integerArrayList.add(2);
integerArrayList.add(3);
integerArrayList.add(4);
//print integerArrayList collection by iterator
for (Integer item:integerArrayList
) {
System.out.println(item);
}
//print integerArrayList collection by index
for (int i=0;i<integerArrayList.size();i++){
System.out.println(integerArrayList.get(i));
}
//print array. We have NO iterator for array!
/*
for (int item:
) {
}
*/
//only simple for
for (int i=0;i<ints.length;i++){
System.out.println(ints[i]);
}
}
}