Site icon JavaGoal

ArrayList()

ArrayList(): It is a default constructor of ArrayList java class. It is used to create an empty ArrayList. It’s initial default capacity 10.  

ArrayList<E> NameOfArrayList =  new ArrayList<E>();

Where, E represents the type of elements in ArrayList.

ArrayList<String> listOfNames =  new ArrayList<String>();

Let say you want to create an ArrayList java and add some names in ArrayList. In other means, java array to list javascript. You can create it by default constructor:

import java.util.ArrayList;
public class ExampleOfArrayList 
{
	public static void main(String[] args) 
	{
		ArrayList<String> listOfNames = new ArrayList<String>();
		listOfNames.add("JAVA");
		listOfNames.add("GOAL");
		listOfNames.add("JAVA");
		listOfNames.add("GOAL");
                for(String name : listOfNames)
		    System.out.println(name);
	}
}

Output: JAVA
GOAL
JAVA
GOAL

Exit mobile version