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// 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//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//create ArrayList
2ArrayList<String> arrayList = new ArrayList<String>();
3//add item to ArrayList
4arrayList.add("item");
5//check if ArrayList contains item (returns boolean)
6System.out.println(arrayList.contains("item"));
7//remove item from ArrayList
8arrayList.remove("item");
1// Java program to demonstrate working of remove
2// on an integer arraylist
3import java.util.List;
4import java.util.ArrayList;
5
6public class GFG
7{
8 public static void main(String[] args)
9 {
10 List<Integer> al = new ArrayList<>();
11 al.add(10);
12 al.add(30);
13 al.add(1);
14 al.add(2);
15
16 // This makes a call to remove(int) and
17 // removes element 20.
18 al.remove(1);
19
20 // Now element 30 is moved one position back
21 // So element 30 is removed this time
22 al.remove(1);
23
24 System.out.println("Modified ArrayList : " + al);
25 }
26}