-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntro.java
More file actions
43 lines (31 loc) · 1.18 KB
/
Intro.java
File metadata and controls
43 lines (31 loc) · 1.18 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
//import java.util.ArrayList;
import java.util.ArrayList;
public class Intro {
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<>();
//adding items to ArrayList
list.add("String 0");
list.add("String 1");
list.add("String 2");
list.add("String 3");
// String s1 = list.get(1);
// System.out.println(list.get(0));
// System.out.println(s1);
//to remove :
list.remove(2); // now it's : 0, 1, 3 (not 2); at 2nd index, it's "String 3; comes one shift right
//to add
list.add(1,"been added; not replaced or overridden"); //moving the items to one shift right
list.add(4,null); //null can be set as well
list.add(2,""); //empty can be set as well
System.out.println("size is : "+ list.size());
for (int i=0; i<list.size(); i++) {
System.out.println(list.get(i));
}
list.clear(); //clears everything
System.out.println(list.size());
for (int i=0; i<list.size(); i++) {
System.out.println(list.get(i)); //prints nothing as everything has been cleared
}
}
}