-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrList.java
59 lines (41 loc) · 1.6 KB
/
arrList.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
package arrayPrograms;
import java.util.ArrayList;
import java.util.Arrays;
public class arrList {
public static void main(String[] args) {
/*
// TODO Auto-generated method stub
//Normal arrays in which we need to specify the size for array
int[] myArray = new int[5];
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;
myArray[3] = 4;
myArray[4] = 5;
System.out.println("Accessing arrays:");
System.out.println(Arrays.toString(myArray));
/* We cannot add more elements to array myArray as it is fixed size, otherwise we will get an error.*/
//ArrayList we need not to specify size in ArrayList
ArrayList < Integer > arrayList = new ArrayList < Integer > ();
arrayList.add(1);
arrayList.add(2);
arrayList.add(3);
arrayList.add(4);
// We can add more elements to arrayList
System.out.println("Accessing ArrayList:");
System.out.println(arrayList);
arrayList.removeAll(arrayList);
System.out.println(arrayList);
/* // primitive types allowed in an array
int[] array = new int[3];
// Objects are also allowed in an array
arrList[] array1 = new arrList[3];
// Not allowed to add primitive type-below line gives compiler error)
ArrayList < char > arrayList = new ArrayList < char > ();
// This is Allowed in ArrayList
ArrayList < Integer > arrayList1 = new ArrayList < >();
ArrayList < String > arrayList2 = new ArrayList < >();
ArrayList < Object > arrayList3 = new ArrayList < >();
*/
}
}