1// java program on ArrayList remove(int index) method example
2import java.util.ArrayList;
3import java.util.List;
4public class ArrayListRemoveIndex
5{
6 public static void main(String[] args)
7 {
8 List<Integer> al = new ArrayList<>();
9 al.add(56);
10 al.add(28);
11 al.add(39);
12 al.add(59);
13 al.add(82);
14 System.out.println("Before using ArrayList.remove(int index) method: " + al);
15 // removes element 28
16 al.remove(1);
17 // element 39 is now moved one position back
18 // So element 39 is removed
19 al.remove(1);
20 System.out.println("After using ArrayList.remove(int index) method: " + al);
21 }
22}
1// Java.util.ArrayList.remove(Object) method example
2import java.util.ArrayList;
3import java.util.List;
4public class ArrayListRemoveObjectMethod
5{
6 public static void main(String[] args)
7 {
8 List<Integer> al = new ArrayList<>();
9 al.add(56);
10 al.add(28);
11 al.add(39);
12 al.add(59);
13 al.add(82);
14 System.out.println("Before using ArrayList.remove(Object) method size of ArrayList: " + al);
15 // removes element 56
16 al.remove(new Integer(56));
17 // removes element 28
18 al.remove(new Integer(28));
19 System.out.println("After using ArrayList.remove(Object) method size of ArrayList: " + al);
20 }
21}
1//Create the ArrayList
2List<Integer> al = new ArrayList<>();
3//Add the items
4al.add(10);
5al.add(18);
6//Remove item(1 = 2and item in list)
7al.remove(1);
1// Given a list of Integers 1, 2, 3, 4, 5, 6 ....etc. remove all values greater than 100.
2 public static void main(String[] args) {
3
4ArrayList<Integer>list1=new ArrayList<>(Arrays.asList(1,2,3,50,70,80,120,150,200));
5 ArrayList<Integer>list2 = new ArrayList<>();
6 for(int each : list1)
7 if(each<100)
8 list2.add(each);
9
10 list1 = list2;
11 System.out.println(list1); // [1, 2, 3, 50, 70, 80]
12 }
1public static void main(String[] args) {
2
3ArrayList<String> names = new ArrayList<>(Arrays.asList("Ahmed", "John", "Eric", "Ahmed"));
4 names.removeAll( Arrays.asList("Ahmed"));
5 System.out.println(names); // [John, Eric]