-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListOperations.java
More file actions
105 lines (70 loc) · 2.87 KB
/
ArrayListOperations.java
File metadata and controls
105 lines (70 loc) · 2.87 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
95
96
97
98
99
100
101
102
103
104
105
import java.util.ArrayList;
public class ArrayListOperations {
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(10);
arrayList.add(20);
arrayList.add(30);
arrayList.add(40);
arrayList.add(50);
System.out.println("Original ArrayList: " + arrayList);
for (int i = 0; i < arrayList.size(); i++) {
System.out.println(arrayList.get(i));
}
insertAtStart(arrayList, 5);
System.out.println("After inserting 5 at the start: " + arrayList);
insertAtEnd(arrayList, 60);
System.out.println("After inserting 60 at the end: " + arrayList);
insertAtIndex(arrayList, 2, 15);
System.out.println("After inserting 15 at index 2: " + arrayList);
deleteAtStart(arrayList);
System.out.println("After deleting element at the start: " + arrayList);
deleteAtEnd(arrayList);
System.out.println("After deleting element at the end: " + arrayList);
deleteAtIndex(arrayList, 3);
System.out.println("After deleting element at index 3: " + arrayList);
deleteByValue(arrayList, 20);
System.out.println("After deleting element with value 20: " + arrayList);
}
public static void insertAtStart(ArrayList<Integer> list, int element) {
list.add(0, element);
}
public static void insertAtEnd(ArrayList<Integer> list, int element) {
list.add(element);
}
public static void insertAtIndex(ArrayList<Integer> list, int index, int element) {
if (index >= 0 && index <= list.size()) {
list.add(index, element);
} else {
System.out.println("Index out of bounds");
}
}
public static void deleteAtStart(ArrayList<Integer> list) {
if (!list.isEmpty()) {
list.remove(0);
} else {
System.out.println("List is empty");
}
}
public static void deleteAtEnd(ArrayList<Integer> list) {
if (!list.isEmpty()) {
list.remove(list.size()-1);
} else {
System.out.println("List is empty");
}
}
public static void deleteAtIndex(ArrayList<Integer> list, int index) {
if (index >= 0 && index < list.size()) {
list.remove(index);
} else {
System.out.println("Index out of bounds");
}
}
public static void deleteByValue(ArrayList<Integer> list, int value) {
if (list.contains(value)) {
list.remove(Integer.valueOf(value));
} else {
System.out.println("Value not found in list");
}
}
}