Repository navigation
Expand file tree
/
Copy pathunit7_1_0.java
More file actions
94 lines (73 loc) · 3.22 KB
/
Copy pathunit7_1_0.java
File metadata and controls
94 lines (73 loc) · 3.22 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
import java.util.ArrayList;
public class unit7_1_0 {
public static void main(String[] args) {
// Allow all data types
ArrayList arrList = new ArrayList();
arrList.add(1000);
arrList.add("Harini");
arrList.add(3000);
System.out.println(arrList);
// Allow Integer only
ArrayList<Integer> aList = new ArrayList<Integer>();
aList.add(1000);
// aList.add("Harini"); // Error
aList.add(3000);
aList.add(600);
aList.add(786);
aList.add(766);
System.out.println("aList after adding values: " + aList);
// Set value at index 3
aList.set(3, 2370);
System.out.println("aList after setting index 3: " + aList);
// Get the value at index 4
Integer valueAt4 = aList.get(4);
System.out.println("aList.get(4): " + valueAt4);
// Remove value at index 2
Integer removedValue = aList.remove(2);
System.out.println("Removed Value of index 2: " + removedValue);
System.out.println("aList after removing index 2: " + aList);
// Remove value 766
boolean removed = aList.remove(valueAt4);
System.out.println("Is the value '" + valueAt4 + "' removed?: " + removed);
System.out.println("aList after removing '" + valueAt4 + "': " + aList);
// contains()
System.out.println("Contains 10,000? " + aList.contains(10000));
System.out.println("Contains 1000? " + aList.contains(1000));
aList.clear();
System.out.println("aList after clear: " + aList);
// contains()
System.out.println("Contains 10,000? " + aList.contains(10000));
System.out.println("Contains 1000? " + aList.contains(1000));
// String array list
ArrayList<String> strList = new ArrayList<String>();
strList.add("Harini");
strList.add("Baskar");
strList.add("Vadivu");
strList.add("Diya");
strList.add("Aathaa");
// print strArray
System.out.println("strList after adding values: " + strList);
// Remove String Baska
removed = strList.remove("Baska");
System.out.println("Is the String 'Baska' removed?: " + removed);
System.out.println("strList after removing 'Baska': " + strList);
// Remove tring Baskar
removed = strList.remove("Baskar");
System.out.println("Is the String 'Baskar' removed?: " + removed);
System.out.println("strList after removing 'Baskar': " + strList);
System.out.println("Size of strList: " + strList.size());
// Multi dimensional array (NOT ArrayList)
int[] a = {1, 2, 3, 4, 5} ;
int[] b = {6, 7, 8};
int[] c = {9, 10};
int[][] arr = {a, b, c};
System.out.println("b[1]: " + b[1]);
System.out.println("arr[1][1]: " + arr[1][1]);
System.out.println("arr[2][1]: " + arr[2][1]);
// Length of the array
System.out.println("Length arr: " + arr.length);
System.out.println("Length arr[0]: " + arr[0].length);
System.out.println("Length arr[1]: " + arr[1].length);
System.out.println("Length arr[2]: " + arr[2].length);
}
}