dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Jun 18, 2021

How to Use Java ArrayList

The Java ArrayList is a class located in the java.util package. It differs from a traditional, built-in array in a very fundamental way: ArrayList allows you to change the size of an array by adding and removing elements. Built-in arrays do not allow for this functionality.

Here is an example of how to create an ArrayList object in Java:

// import the ArrayList class
import java.util.ArrayList;

// Create the Main function

public class Main {
	public static void main(String[] args) {

//Creating an Arraylist object
	ArrayList superHeroes = new ArrayList();
	superHeroes.add("Big-Head Man!");
	superHeroes.add("Developer Man!");
	superHeroes.add("Lot-of-Legs-Woman!");
	superHeroes.add("Captain Mustache!");
	superHeroes.add("Wonder Wombat!");
//Printing elements of the ArrayList
	System.out.println(superHeroes);
	}
}

When you run this Java ArrayList code, you get the following output:

[Big-Head Man!, Developer Man!, Lot-of-Legs-Woman!, Captain Mustache!, Wonder Wombat!]
James Payne
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date