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
//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
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);
}
}