Back to articles
ArrayList

ArrayList

via Dev.to BeginnersIndumathy

ArrayList Class ArrayList is one of the most commonly used classes in Java. It implements the List interface. Package: java.util Syntax ArrayList name = new ArrayList(); Features of ArrayList Allows duplicate values Maintains insertion order Allows multiple null values Dynamic size (it grows automatically) Difference Between remove() and clear() remove() Removes a specific element from the list. clear() Removes all elements from the list. Iterating Through List We can use a for-each loop to iterate through elements. for(String name : list){ System.out.println(name); } Let's See some common methods: Lets create the student name list by ArrayList and make changes. 1.add() Add Student Names Using ArrayList import java.util.ArrayList; import java.util.List; public class Demo { public static void main(String[] args) { ArrayList<String> std = new ArrayList<String>(); std.add("Hari"); std.add("Indu"); std.add("Kevin"); System.out.println(std); } } Output [Hari, Indu, Kevin] 2. add(index,"name

Continue reading on Dev.to Beginners

Opens in a new tab

Read Full Article
2 views

Related Articles