remove item from arraylist java code example
Example 1: remove arraylist index in for loop
for (int i = data.size()-1; i >= 0; i--){
if (data.get(i).contains(element)){
data.remove(i);
}
}
Example 2: How to remove element from arraylist in java
// Java.util.ArrayList.remove(Object) method example
import java.util.ArrayList;
import java.util.List;
public class ArrayListRemoveObjectMethod
{
public static void main(String[] args)
{
List<Integer> al = new ArrayList<>();
al.add(56);
al.add(28);
al.add(39);
al.add(59);
al.add(82);
System.out.println("Before using ArrayList.remove(Object) method size of ArrayList: " + al);
// removes element 56
al.remove(new Integer(56));
// removes element 28
al.remove(new Integer(28));
System.out.println("After using ArrayList.remove(Object) method size of ArrayList: " + al);
}
}
Example 3: How to remove element from arraylist in java
// java program on ArrayList remove(int index) method example
import java.util.ArrayList;
import java.util.List;
public class ArrayListRemoveIndex
{
public static void main(String[] args)
{
List<Integer> al = new ArrayList<>();
al.add(56);
al.add(28);
al.add(39);
al.add(59);
al.add(82);
System.out.println("Before using ArrayList.remove(int index) method: " + al);
// removes element 28
al.remove(1);
// element 39 is now moved one position back
// So element 39 is removed
al.remove(1);
System.out.println("After using ArrayList.remove(int index) method: " + al);
}
}
Example 4: remove item from arraylist in java
//Create the ArrayList
List<Integer> al = new ArrayList<>();
//Add the items
al.add(10);
al.add(18);
//Remove item(1 = 2and item in list)
al.remove(1);
Example 5: java arraylist remove
//create ArrayList
ArrayList<String> arrayList = new ArrayList<String>();
//add item to ArrayList
arrayList.add("item");
//check if ArrayList contains item (returns boolean)
System.out.println(arrayList.contains("item"));
//remove item from ArrayList
arrayList.remove("item");
Example 6: how to remove an element from an arraylist java
// Java program to demonstrate working of remove
// on an integer arraylist
import java.util.List;
import java.util.ArrayList;
public class GFG
{
public static void main(String[] args)
{
List<Integer> al = new ArrayList<>();
al.add(10);
al.add(30);
al.add(1);
al.add(2);
// This makes a call to remove(int) and
// removes element 20.
al.remove(1);
// Now element 30 is moved one position back
// So element 30 is removed this time
al.remove(1);
System.out.println("Modified ArrayList : " + al);
}
}