Subscribe To MegiSoft RSS Feeds!     Your Browser OR  Google Feedburner

Java ArrayList

ArrayList is one of the classes of the Java standard library. It is a subclass of the class AbstractList and implements the interfaces, List<E>, Collection<E>, Serializable, Cloneable, Iterable<E>, RandomAccess. ArrayList is resizable unlike Arrays. It is the new version of the Vector class, but it is unsynchronized. It permits all elements including null

The following UML diagram shows the position of the class ArrayList in the hierarchy.

Articles: ArrayList  HashSet  TreeSet  HashMap  TreeMap 
Quizzes: Java Collection Quizzes 

arrarraylist-hierarchy

 

What is an ArrayList in Java?

An ArrayList is a data structure that allows you to collect elements in a list. It is one of the classes in the Java library, which contains different methods to add, remove, sort elements in alphabetical order and more. Each element of an ArrayList has an index which starts from 0.  To use it in your own classes, you need to import the package “java.util” from the Java library  as shown below.

import java.util.ArrayList;

 

Is ArrayList my best Java collection class choice?

To answer the question whether ArrayList is your best choice,you need to know more details about it as well as about other Java collection classes.  The information and the exercises below will help you to make that decision.

  1. ArrayList implements List interface
  2. ArrayList is resizable.
  3. ArrayList permits all elements, including null.
  4. ArrayList allows duplicates.
  5. ArrayList is unsynchronized.
  6. ArrayList is an ordered collection.
  7. ArrayList is index based. You can retrieve, remove or add objects to a specific index position by using get(index), remove(index) and add(index, element).

The following exercises show some important methods of what you can do with the elements of an ArrayList.

 

Exercise 1A: How to create a Java ArrayList and add elements to it?

  • Create an ArrayList “nameList” and add the following names to it: Emily, William, Sofia, Lucas, Emma, Jack.

Exercise 1B: How to find the size of a Java ArrayList?

  • Print the size of the ArrayList to the standard output before and after adding the names.

Answer 1

  • A- The following code adds the mentioned names to the ArrayList nameList.
  • B- The program writes the following to the standard output when it is compiled and run.

nameList size before adding the names: 0
nameList size after adding the names: 6

 

import java.util.ArrayList;

public class TestArrayList {

	public static void main(String[] args){
		ArrayList nameList = new ArrayList();
		System.out.println("nameList size before adding the names: " + nameList.size());

		nameList.add("Emily");
		nameList.add("William");
		nameList.add("Sofia");
		nameList.add("Lucas");
		nameList.add("Emma");
		nameList.add("Jack");
		System.out.println("nameList size after adding the names: " + nameList.size());
	}
}

Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

Exercise 2: How to retrieve the elements of a Java ArrayList and print them to the standard output?

  • Add a piece of code to the exercise 1 to retrieve and print all the names to the standard output.

Answer 2

  • When the program is compiled and run, it writes the following names to the standard output.

Print the names to the standard output
Emily William Sofia Lucas Emma Jack
Another method to print the names to the standard output
Emily William Sofia Lucas Emma Jack

 

import java.util.ArrayList;
import java.util.Iterator;

public class TestArrayList {

	public static void main(String[] args){
		ArrayList nameList = new ArrayList();
		nameList.add("Emily");
		nameList.add("William");
		nameList.add("Sofia");
		nameList.add("Lucas");
		nameList.add("Emma");
		nameList.add("Jack");

		System.out.println("Print the names to the standard output");
		for(int i = 0; i < nameList.size(); i ++ ) {
			System.out.print(" " + nameList.get(i));
		}
		System.out.println("");
		System.out.println("Another method to print the names to the standard output");
		Iterator<String> iter = nameList.iterator();
		while(iter.hasNext()) {
			System.out.print(" " + iter.next());
		}
	}
}

Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

Exercise 3: How to add an element to a specific index position of a Java ArrayList?

  • Add the name “John” between the two names “Sofia” and “Lucas”

Answer 3

  • When the program is compiled and run, it writes the following names to standard output.

Emily William Sofia John Lucas Emma Jack

import java.util.ArrayList;

public class TestArrayList {

	public static void main(String[] args){
		ArrayList nameList = new ArrayList();
		nameList.add("Emily");
		nameList.add("William");
		nameList.add("Sofia");
		nameList.add("Lucas");
		nameList.add("Emma");
		nameList.add("Jack");

		// Add the name "John" between the names "Sofia" and "Lucas"
		nameList.add(3,"John");

		for(int i = 0; i < nameList.size(); i ++ ) {
			System.out.print(" " + nameList.get(i));
		}
	}
}

Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

Exercise 4: How to remove an element from a specific position of a Java ArrayList?

  • Remove the name “Emma” at position index 4 from the list. Remember that an ArrayList index starts from 0.

Answer 4

  • When the program is compiled and run, it writes the following names to standard output.

 Emily William Sofia Lucas Jack

import java.util.ArrayList;

public class TestArrayList {

	public static void main(String[] args){
		ArrayList nameList = new ArrayList();
		nameList.add("Emily");
		nameList.add("William");
		nameList.add("Sofia");
		nameList.add("Lucas");
		nameList.add("Emma");
		nameList.add("Jack");

		// Remove Emma from the list
		nameList.remove(4);

		for(int i = 0; i < nameList.size(); i ++ ) {
			System.out.print(" " + nameList.get(i));
		}
	}
}

Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

Exercise 5: How to sort the elements of a Java ArrayList in an alphabetical order?

  • Sort the names of the ArrayList nameList in an alphabetical order.

Answer 5

  • When the program is compiled and run, it writes the following names to  standard output.

 Emily Emma Jack Lucas Sofia William

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

public class TestArrayList {

	public static void main(String[] args){
		ArrayList nameList = new ArrayList();
		nameList.add("Emily");
		nameList.add("William");
		nameList.add("Sofia");
		nameList.add("Lucas");
		nameList.add("Emma");
		nameList.add("Jack");

		// Sort the names in alphabetical order.
		Collections.sort(nameList);

		for(int i = 0; i < nameList.size(); i ++ ) {
			System.out.print(" " + nameList.get(i));
		}
	}
}

Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

Exercise 6: How to search an element in a Java ArrayList?

  • Search the two names Ben and Emma in the ArrayList “nameList”.  To check that we need to use the boolean method contains (object element). The method returns true if the ArrayList contains the object otherwise return false.

Answer 6

Does nameList contain Ben ? false
Does nameList contain Emma ? true

 

import java.util.ArrayList;

public class TestArrayList {

	public static void main(String[] args){
		ArrayList nameList = new ArrayList();
		nameList.add("Emily");
		nameList.add("William");
		nameList.add("Sofia");
		nameList.add("Lucas");
		nameList.add("Emma");
		nameList.add("Jack");

		// Check whether nameList contains Ben and Emma 
		boolean foundBen = nameList.contains("Ben");
		System.out.println("Does nameList contain Ben ? " + foundBen);
		boolean foundEmma = nameList.contains("Emma");
		System.out.println("Does nameList contain Emma ? " + foundEmma);
	}
}

Please, leave your questions, feedback and suggestions in the comments below! If you need more explanation by any of the answers, just let me know to update it for you!

Exercise 7: How to find an element in a specific position of a Java ArrayList?

  • To find any element in a specific position of an ArrayList, you can use the method get(int).
    find the element in the second index position.

Answer 7

  • The program writes “Sofia” to the standard output when it is compiled and run?

 

import java.util.ArrayList;

public class TestArrayList {

	public static void main(String[] args){
		ArrayList nameList = new ArrayList();
		nameList.add("Emily");
		nameList.add("William");
		nameList.add("Sofia");
		nameList.add("Lucas");
		nameList.add("Emma");
		nameList.add("Jack");
		// find the element in the second index position 
		System.out.print(nameList.get(2));
	}
}
Megisoft.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes. The author of Megisoft is Sar Maroof who has more than a decade of experience with web development mainly with Java, MySql, HTML and more..
Articles: ArrayList  HashSet  TreeSet  HashMap  TreeMap 
Quizzes: Java Collection Quizzes 

Suggested Articles:


Click Here For Info about: Sar Maroof's New Java Book
Author: Sar Maroof
More than a decade of experience with developing web applications mainly with Java.
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster - Mind IT
Applied Science in Physics HBO Amsterdam
Bachellor Science in Physics at Basra university.
 Subscribe To Megisoft RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!
Share on FacebookTweet about this on TwitterShare on RedditShare on LinkedIn

5 Comments

Add a Comment
  1. Thanks for this article it is easy to understand!

  2. Hello Sar, great blog. I love your quizzes!

  3. This is a neat surymma. Thanks for sharing!

  4. Thanks great post!

    1. You are welcome!

Leave a Reply

megisoft © 2016