What is arraylist: In the previous article, we have seen Java Program to Synchronize an ArrayList
In this article we will know what is ArrayList in Java.
What is ArrayList in Java?
ArrayList:
ArrayList in Java is like an array with no size limit means type of resizable array. It is a part of collection framework and it is present in java.util
package. ArrayList class in Java implements List interface which extends Collection interface.
ArrayList (class) —extends—> AbstractList (class) —implements—> List (interface) —extends—> Collection (interface) —extends—> Iterable (interface)
Creating an ArrayList:
ArrayList<Type> SampleArrayList= new ArrayList<>();
Where,
Type
represents the type of arraylist.SampleArrayList
is name of the arraylist.
Example:
ArrayList<Integer> arrayList = new ArrayList<>(); //ArrayList of Integer type ArrayList<String> arrayList = new ArrayList<>(); //ArrayList of String type
Let’s see an program to understand it more clearly.
Program:
import java.util.ArrayList; class Main { public static void main(String[] args) { //ArrayList created ArrayList<String> city = new ArrayList<>(); // Adding elements to the ArrayList city.add("Chennai"); city.add("Kolkata"); city.add("Bhubaneswar"); city.add("Hyderabad"); city.add("Banglore"); city.add("Pune"); city.add("Gurgaon"); city.add("Bhopal"); System.out.println("ArrayList: " + city); } }
Output: ArrayList: [Chennai, Kolkata, Bhubaneswar, Hyderabad, Banglore, Pune, Gurgaon, Bhopal]
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.
Related Java Programs: