-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathH1 Arraylist.java
52 lines (32 loc) · 1.17 KB
/
H1 Arraylist.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
import java.util.ArrayList;
public class Arraylist {
public static void main(String[] kuchVi) {
// ArrayList
ArrayList<String> names = new ArrayList<String>();
names.add("Joe");
names.add("Kate");
System.out.println(names.size());
for (int i = 0; i < names.size(); i++) {
// getter
System.out.println("Name: " + names.get(i));
}
names.set(0, "Subhranshu Choudhury");
names.add(2, "Offelia");
System.out.println(names.get(2));
names.remove(2);
names.clear();
System.out.println(names.size());
// System.out.println(names.get(0));
// Array vs ArrayList
// - use arrays when size is fixed.
// - use ArrayList where size can vary.
// ArrayList<int> numbers = new ArrayList<int>(); -- error ArrayList can only store objects.
// ArrayList<Integer> numbers = new ArrayList<Integer>(); -- correct
// ArrayList can only store objects.
}
}
// ignore -- previous recap
/*
* Wrapper: immutable class that wraps around a primitive.
* Integer - immutable class that wraps around int.
*/