Write a Java program to create a new array list, add some elements and print out the collection


This Java program creates an ArrayList of String type named list_Str and adds seven elements to it using the add() method of ArrayList. The elements added to the list are "Apple", "Banana", "Cherry", "Pineapple", "Guava", "Papaya" and "Plum". Finally, the program prints the elements of the list using the println() method of System.out object.

Source Code

import java.util.*;
class Add_Elements
{
	public static void main(String[] args)
	{
		List<String> list_Str = new ArrayList<String>();
		list_Str.add("Apple");
		list_Str.add("Banana");    
		list_Str.add("Cherry");
		list_Str.add("Pineapple");
		list_Str.add("Guava");
		list_Str.add("Papaya");
		list_Str.add("Plum");
		System.out.println(list_Str);
	}
}

Output

[Apple, Banana, Cherry, Pineapple, Guava, Papaya, Plum]

Example Programs