Constructors
Must Read Type Conversion in Java and Swap Function in Java
Methods

Must Read Difference between ArrayList and LinkedList, Duck Number in Java
Basic operations
Add/Update elements
import java.util.ArrayList;
public class Main {
public static void main(String[] args){
// create ArrayList
ArrayList<String> languages = new ArrayList<>();
// add() method without the index parameter
languages.add("Arabic");
languages.add("Chinese");
languages.add("Hindi");
System.out.println("List: " + languages);
languages.set(1,"English");
languages.replaceAll(e -> e.toUpperCase());
System.out.println("List: " + languages);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
List: [Arabic, Chinese, Hindi]
List: [ARABIC, ENGLISH, HINDI]
Access elements
import java.util.ArrayList;
public class Main {
public static void main(String[] args){
ArrayList<String> languages = new ArrayList<>();
languages.add("Arabic");
languages.add("Malayalam");
languages.add("Hindi");
System.out.println("1: " + languages.get(0));
System.out.println("2: " + languages.get(1));
System.out.println("3: " + languages.get(2));
languages.forEach((e) -> {
e = e.toUpperCase();
System.out.print(e + "\n");
});
System.out.println("Indian Languages: " + languages.subList(1,3));
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
1: Arabic
2: Malayalam
3: Hindi
ARABIC
MALAYALAM
HINDI
Indian Languages: [Malayalam, Hindi]
You can compile java code just by clicking here.
Remove elements
import java.util.ArrayList;
public class Main {
public static void main(String[] args){
ArrayList<String> languages = new ArrayList<>();
languages.add("Arabic");
languages.add("Malayalam");
languages.add("Hindi");
System.out.println("Languages: " + languages);
System.out.println("Removing " + languages.remove(0));
System.out.println("Removing Malayalam...");
languages.removeIf(e -> e.startsWith("Mal"));
System.out.println("Languages: " + languages);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
Languages: [Arabic, Malayalam, Hindi]
Removing Arabic
Removing Malayalam...
Languages: [Hindi]
Try this code by yourself on Online Java Compiler.
Must Read: Java List Iterator and Hashcode Method in Java
FAQs
-
What is the difference between an array and ArrayList?
An array is a basic data structure provided by Java that has a fixed length. In contrast, ArrayList is a class of Java Collections framework that has a variable length. An Array can store values of primitive data types while ArrayList uses Wrapper classes.
-
What is the difference between iterator and listIterator?
Iterator can traverse the array elements in the forward direction and can perform only remove operations. ListIterator can traverse the array of elements in backward as well as forward directions. It can perform add, remove, and set operations while traversing the collection.
Conclusion
This article extensively discusses ArrayList in Java. We hope that this blog has helped you enhance your knowledge about the different methods and ways of implementing ArrayLists in Java. If you would like to learn more, check out our articles Arrays and Wrapper classes.
Recommended problems -
Explore our Coding Ninjas Library for more articles like these and do upvote our blog to help other ninjas grow. Happy Coding!