Creating Array in Java

Creating Array in Java for the first time seems frustrating there are Map, HashMap, List, and ArrayList. Those are making us wonder which one to use. So, for the easy one I chose List as an object to create array. You have to create a java file named MyList.java first to make it works. Here is a simple script to make the array in Java.

import java.util.ArrayList;
import java.util.List;

class MyList{
	
	public static void main(String[] args){
		List<String> myList = new ArrayList<String>();
		myList.add("String 1");
		myList.add("String 2");
		
		for(String entry: myList){
			System.out.println(entry);
		}
	}
	
}

The question arises, why do we use List<String> myList = new ArrayList<String>() instead of ArrayList<String> myList = new ArrayList<String>() ?

You can get the answer from Difference Between List and ArrayList ? article and Stackoverflow answer as well.

Leave a Reply