xxxxxxxxxx
//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);
xxxxxxxxxx
//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");
xxxxxxxxxx
// Instantiate an arraylist of integers.
List<Integer> al = new ArrayList<>();
// Append elements.
al.add(10);
al.add(30);
al.add(1);
al.add(2);
// Remove the element at position 1 (remember that 0 is the first position).
// This removes the element 30.
al.remove(1);
xxxxxxxxxx
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>(Arrays.asList("Ahmed", "John", "Eric", "Ahmed"));
names.removeAll( Arrays.asList("Ahmed"));
System.out.println(names); // [John, Eric]
xxxxxxxxxx
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
ArrayList<String> animals = new ArrayList<>();
// add elements in the array list
animals.add("Dog");
animals.add("Cat");
animals.add("Horse");
System.out.println("ArrayList: " + animals);
// remove element from index 2
String str = animals.remove(2);
System.out.println("Updated ArrayList: " + animals);
System.out.println("Removed Element: " + str);
}
}
xxxxxxxxxx
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
ArrayList<String> animals = new ArrayList<>();
// add elements in the arraylist
animals.add("Peacock");
animals.add("Markhor");
animals.add("Cow");
System.out.println("ArrayList: " + animals +"\n");
// remove element from index 2
String str = animals.remove(2);
System.out.println("Updated ArrayList: " + animals);
System.out.println("Removed Element: " + str);
}
}
xxxxxxxxxx
// Given a list of Integers 1, 2, 3, 4, 5, 6 ....etc. remove all values greater than 100.
public static void main(String[] args) {
ArrayList<Integer>list1=new ArrayList<>(Arrays.asList(1,2,3,50,70,80,120,150,200));
ArrayList<Integer>list2 = new ArrayList<>();
for(int each : list1)
if(each<100)
list2.add(each);
list1 = list2;
System.out.println(list1); // [1, 2, 3, 50, 70, 80]
}