-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraylist.java
More file actions
34 lines (32 loc) · 1000 Bytes
/
Arraylist.java
File metadata and controls
34 lines (32 loc) · 1000 Bytes
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
package com.company;
import java.util.ArrayList;
public class Arraylist {
public static void main(String[] args) {
ArrayList<Integer> l1 = new ArrayList<>();
ArrayList<Integer> l2 = new ArrayList<>(5);
l2.add(28);
l2.add(20);
l2.add(5);
l1.add(5);
// l1.clear();
l1.add(6);
l1.add(0,1);
l1.addAll(l2);
l1.set(2,89);
l1.remove(0);
System.out.println("Size of ArrayList : " + l1.size());
System.out.println(l1.contains(5));
System.out.println(l1.indexOf(5));
System.out.println(l1.lastIndexOf(5));
System.out.println(l1.isEmpty());
// Accessing elements via foreach
/* for (int i: l1) {
System.out.println(i);
} */
// Accessing elements via for
for (int i=0; i<l1.size(); i++) {
System.out.print(l1.get(i));
System.out.print(", ");
}
}
}