Skip to content

Commit 2020847

Browse files
author
JavaProgramTo.com
committed
Java ArrayList Insert/Replace At Index
1 parent 6a3f0dc commit 2020847

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.javaprogramto.java8.arraylist.insert;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class ArrayListInsertAddExample {
7+
8+
public static void main(String[] args) {
9+
10+
List<Integer> list1 = new ArrayList<>();
11+
12+
list1.add(10);
13+
list1.add(20);
14+
list1.add(30);
15+
list1.add(40);
16+
list1.add(50);
17+
list1.add(60);
18+
19+
System.out.println("List values before insertion - " + list1);
20+
System.out.println("list1 size before - "+list1.size());
21+
22+
list1.add(3, 333);
23+
24+
System.out.println("List values after inserting the value 333 at index 3 - " + list1);
25+
System.out.println("list1 size after - "+list1.size());
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.javaprogramto.java8.arraylist.insert;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class ArrayListInsertAddExample2 {
7+
8+
public static void main(String[] args) {
9+
10+
List<String> list2 = new ArrayList<>();
11+
12+
list2.add("one");
13+
list2.add("two");
14+
list2.add("three");
15+
list2.add("four");
16+
list2.add("five");
17+
list2.add("six");
18+
19+
System.out.println("List2 values before insertion - " + list2);
20+
System.out.println("list2 size before - "+list2.size());
21+
22+
list2.add(3, "new four");
23+
24+
System.out.println("List2 values after inserting the value 333 at index 3 - " + list2);
25+
System.out.println("list2 size after - "+list2.size());
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.javaprogramto.java8.arraylist.insert;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class ArrayListUpdateSetExample3 {
7+
8+
public static void main(String[] args) {
9+
10+
List<Integer> list1 = new ArrayList<>();
11+
12+
list1.add(10);
13+
list1.add(20);
14+
list1.add(30);
15+
list1.add(40);
16+
list1.add(50);
17+
list1.add(60);
18+
19+
System.out.println("List1 values before update - " + list1);
20+
System.out.println("list1 size before - "+list1.size());
21+
22+
list1.set(3, 333);
23+
24+
System.out.println("List1 values after updating index 3 value with new value 333 - " + list1);
25+
System.out.println("list1 size after - "+list1.size());
26+
}
27+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.javaprogramto.java8.arraylist.insert;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class ArrayListUpdateSetIterateExample4 {
7+
8+
public static void main(String[] args) {
9+
10+
List<Integer> list1 = new ArrayList<>();
11+
12+
list1.add(4);
13+
list1.add(10);
14+
list1.add(24);
15+
list1.add(25);
16+
list1.add(5);
17+
list1.add(28);
18+
19+
System.out.println("List1 values before update - " + list1);
20+
System.out.println("list1 size before - " + list1.size());
21+
22+
for (int index = 0; index < list1.size(); index++) {
23+
if (list1.get(index) % 5 == 0) {
24+
list1.set(index, -1);
25+
}
26+
}
27+
28+
System.out.println("List1 values after updating index 3 value with new value 333 - " + list1);
29+
System.out.println("list1 size after - " + list1.size());
30+
}
31+
}

0 commit comments

Comments
 (0)