-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_List.java
More file actions
23 lines (23 loc) · 2 KB
/
Array_List.java
File metadata and controls
23 lines (23 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Basic operations of ARRAY LIST
package arrays;
import java.util.ArrayList;
public class Array_List {
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<>(2);
arr.add(0, 20);
arr.add(1, 30); // Created an Array list [20, 30]
System.out.print("Elements of the array list are: ");
for(int i=0; i<arr.size(); i++){
System.out.print(arr.get(i)+" "); // Printing the elements of the array list
}
System.out.println(" & Size of the array list is: "+arr.size()); // Size of array list
System.out.println("Modyfying the 0th element(20->40)");
arr.set(0, 40); // modify/changing the 0th element
System.out.println("New array list is: "+arr+" & the size of the array list is: "+arr.size()); // printing the modified value & size of array list
System.out.println("Adding a new element(50)");
arr.add(50); // adding a new element(push) & automatically increased the size of the array list(3)
System.out.println("New array list after adding a new element: "+arr+" & the new size of the array list is: "+arr.size()); // printing the array list & size of array list
arr.remove(1); // Removing the 1th element & so the size will decrease from 3 to 2
System.out.println("New array list after removing 1th element: "+arr+" & the new size of the array list is: "+arr.size()); // printing the final array list & size of array list
}
}