Programming Languages: [Java, JavaScript, Python] ArrayList after clear(): [] In the above example, we have created a arraylist namedlanguages. The arraylist stores the name of programming languages. Here, we have used theclear()method to remove all the elements oflanguages. ArrayList clear() ...
ArrayList 1: [JavaScript, Python, Java] ArrayList 2: [Java, Python] ArrayList 1 contains all elements of ArrayList 2: true ArrayList 2 contains all elements of ArrayList 1: false In the above example, we have created two arraylists namedlanguages1andlanguages2. Notice the expression, // retu...
packagecom.programiz.arraylist;importjava.util.ArrayList;importjava.util.Iterator;publicclassIteratorMethod{publicstaticvoidmain(String[] args){ ArrayList<String> animals =newArrayList<>();// Add elements in the array listanimals.add("Dog"); animals.add("Cat"); animals.add("Horse"); animals.add...
ArrayList: [java, javascript, swift, python] Updated ArrayList: [JAVA, JAVASCRIPT, SWIFT, PYTHON] In the above example, we have created an arraylist named languages. Notice the line, languages.replaceAll(e -> e.toUpperCase()); Here, e -> e.toUpperCase()is a lambda expression. It converts...
ArrayList: [Java, Python, JavaScript] Size of ArrayList: 3 In the above example, we have created two arraylists namedlanguages. The arraylist contains 3 elements. Notice the line, languages.trimToSize(); Here, thetrimToSize()method sets the capacity of arraylist equal to the number of elements...
ArrayList 1: [Java, Python] ArrayList 2: [JavaScript, C] Updated ArrayList 2: [JavaScript, Java, Python, C] In the above example, we have two arraylists namedlanguages1andlanguages2. Notice the line, languages2.addAll(1, languages1); ...
The size() method returns the number of elements present in the arraylist. Example import java.util.ArrayList; class Main { public static void main(String[] args) { // create an ArrayList ArrayList<Integer> primeNumbers = new ArrayList<>(); primeNumbers.add(2); primeNumbers.add(3); ...
Example 2: Remove Countries With "land" in Name import java.util.ArrayList; class Main { public static void main(String[] args) { // create an ArrayList ArrayList<String> countries = new ArrayList<>(); // add elements to the ArrayList countries.add("Iceland"); countries.add("America")...
ArrayList: [JavaScript, Java, Python, C] SubList: [Java, Python] In the above example, we have used thesubList()method to get elements from index 1 to 3 (excluding 3). Note: If you want to know how to get the index of the specified element, visitJava ArrayList indexOf(). ...
ArrayList: [JavaScript, Java, Python] ArrayList after remove(): [JavaScript, Java] Removed Element: Python In the above example, we have created an arraylist namedlanguages. Notice the expression, languages.remove(2) Here, theremove()returns and removes the element present at position2(i.e.Pyt...