COMP2247 - Algorithms and Data Structures

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

ArrayList ArrayListProcessing.java NameSearching.java Hardware Store Program With ArrayList MyArrayList Program
MyArrayList.java
MyArrayListClient.java




ArrayList


In this section, we will study ArrayList.

Introduction to ArrayList class

ArrayList is a class in Java Collection Framework.

Like array, it is a data structure containing a number of data elements.

All elements in an ArrayList have the same type.

Unlike array, ArrayList allows only object storage (generics).

ArrayList is dynamic data structure.

It can expand automatically when new objects are added and shrink automatically when objects are removed.

The index of ArrayList cell starts from 0.

We use methods to perform functions such as access, add, remove, and replace the elements in ArrayList.

In fact, ArrayList class has a number of methods to manipulate the ArrayList. Refer to Java API documentation.

Create ArrayList Objects

ArrayList <Integer> intList = new ArrayList<>(); //ArrayLis of Integer objects

Integer is the wrapper class of int.

Integer objects and int primitives can be used interchangeably due to auto boxing and auto unboxing.

Integer n1 = 25; //auto boxing

int k = n1; //auto unboxing

ArrayList <String> nameList = new ArrayList<>(); //ArrayList of String objects

ArrayList <Inventory> inventoryList = new ArrayList<>();

Add Object to ArrayList

add(E obj) method adds an object to the end of the list.

add(int index, E obj) method inserts an object at the specified index.

intList.add( 25 ); //add 25 to the end of the list

intList.add( 0, 8 ); //insert 8 at index 0

nameList.add( "Jon Doe" ); //add "Jon Doe" to the end of the list

inventoryList.add( new Inventory( 23456, "Tool", 25, 35.66 ) ); //add an Inventory object to the list

Get Object from ArrayList

get(int index) method returns the object at the specified index.

int n = intList.get( 0 ); //get the object at index 0

String name = nameList.get( 0 ); //get the object at index 0

Inventory item = inventoryList.get( 0 ); //get the object at index 0

Get objects in the loop

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

Remove Object from ArrayList

remove (int index) method removes and returns the object at the specified index.

int n = intList.remove( 0 ); //remove and return the object at index 0

String name = nameList.remove( 0 ); //remove and return the object at index 0

Inventory item = inventoryList.remove( 0 ); //remove and return the object at index 0

Replace Object in ArrayList

set(int index, E obj) method replaces the object at the specified position and returns the replaced object.

  //replace the object at index 0 with the value of 56
  //and return the replaced object
int n = intList.set( 0, 56 );

  //replace the object at index 2 with "Tom"
  //and return the replaced object
String name = nameList.set( 2, "Tom" );

  //replace the object at index 6 with the new item "Glue"
  //and return the replaced object
Inventory item = inventoryList.set( 6, new Inventory(111, "Glue", 100, 5.99) );

Print ArrayList

System.out.println( intList ); //print all the objects on the same line

Use Enhanced Loop to Work with ArrayList

for(Integer n : intList) {
System.out.println( n ); //print one object per line
}