Home > Tutorial > Java ArrayList Sample Code

Java ArrayList Sample Code

The Java ArrayList collection class is a good option when you need to store a collection of data. The collections class can store just about anything, but it’s best to stay consistent. If you declare an ArrayList to hold Strings, it’s best to stick with only storing Strings.

The Java sample code below will give you a good idea on the basics of implementing an ArrayList in your Java application. The first section is the actual compiled source code and the section a little further down is the output of running the sample code.

Java ArrayList Source Code:

package com.jcd.java.tutorials;

import java.util.ArrayList;
import java.util.Collections;

public class JavaArrayList
{

	public static void main(String[] args) 
	{
		//Create new ArrayList that holds a collection of Strings
		ArrayList<String> stringArrayList = new ArrayList<String>();
		
		//Let's populate the ArrayList with random String values
		
		stringArrayList.add("Delta");
		stringArrayList.add("Charlie");
		stringArrayList.add("Bravo");
		stringArrayList.add("Alpha");
		

		//Let's print our the entire ArrayList
		System.out.println("stringArrayList:"+stringArrayList);
		
		System.out.println("\nUnsorted ArrayList:");
		
		//Let's print out each element in the ArrayList
		for(String myEntry : stringArrayList)
		{
			System.out.println("Each Entry:"+myEntry);
		}
		
		//Let's sort the ArrayList alphabetically
		Collections.sort(stringArrayList);
		
		System.out.println("\nSorted ArrayList:");
		
		//Let's print out each element in the ArrayList
		for(String myEntry : stringArrayList)
		{
			System.out.println("Each Entry:"+myEntry);
		}
		
		//Let's see how many elements are in the ArrayList
		System.out.println("\nNumber of Elements: "+stringArrayList.size());
		
		//Let's add "Foxtrot" to the ArrayList
		stringArrayList.add("Foxtrot");	
		
		System.out.println("\nArrayList After Adding [Foxtrot]:\n");
		
		//Let's print out each element in the ArrayList
		for(String myEntry : stringArrayList)
		{
			System.out.println("Each Entry:"+myEntry);
		}
		
		//Let's add "Foxtrot" to the ArrayList
		stringArrayList.remove("Bravo");	
		
		System.out.println("\nArrayList After Removing [Bravo]:\n");
		
		//Let's sort the ArrayList alphabetically
		Collections.sort(stringArrayList);
		
		//Let's print out each element in the ArrayList
		for(String myEntry : stringArrayList)
		{
			System.out.println("Each Entry:"+myEntry);
		}
		
		//Let's see how many elements are in the ArrayList
		System.out.println("\nNumber of Elements: "+stringArrayList.size());
		
		//Let's remove all the elements from the ArrayList
		stringArrayList.clear();
		
		//Let's see how many elements are in the ArrayList
		System.out.println("\nNumber of Elements After clear(): "+stringArrayList.size());	
	}
}



Sample Java ArrayList Output:

stringArrayList:[Delta, Charlie, Bravo, Alpha]

Unsorted ArrayList:
Each Entry:Delta
Each Entry:Charlie
Each Entry:Bravo
Each Entry:Alpha

Sorted ArrayList:
Each Entry:Alpha
Each Entry:Bravo
Each Entry:Charlie
Each Entry:Delta

Number of Elements: 4

ArrayList After Adding [Foxtrot]:

Each Entry:Alpha
Each Entry:Bravo
Each Entry:Charlie
Each Entry:Delta
Each Entry:Foxtrot

ArrayList After Removing [Bravo]:

Each Entry:Alpha
Each Entry:Charlie
Each Entry:Delta
Each Entry:Foxtrot

Number of Elements: 4

Number of Elements After clear(): 0



References:

Smedley
Posted by

Smedley

Smedley has been doing software development for years, which mainly docues on C and C++. In the past number of years, he has focused on Java programming.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2018 JavaCodeDepot