Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Creating an ArrayList with Multiple Object Types in Java

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

ArrayList class Java is basically a resizable array i.e. it can grow and shrink in size dynamically according to the values that we add to it. It is present in java.util package. 

Syntax: To create an ArrayList of Integer type is mentioned below.

List<Integer> list = new ArrayList <Integer>();

It is more common to create an ArrayList of definite type such as Integer, Double, etc. But there is also a method to create ArrayLists that are capable of holding Objects of multiple Types. 

We will discuss how we can use the Object class to create an ArrayList.

Object class is the root of the class hierarchy. It is being extended by every class in Java either directly or indirectly. If a class doesn’t inherit from any other class then it extends the Object class directly otherwise if it extends any class then it extends the Object class indirectly from its base class. We can use the Object class to declare our ArrayList using the syntax mentioned below. 

ArrayList<Object> list = new ArrayList<Object>();

The above list can hold values of any type. The code given below presents an example of the ArrayList with the Objects of multiple types.


// Java program to create an ArrayList with
// Multiple Object Types in Java
import java.util.ArrayList;
public class GFG {
    public static void main(String[] args)
        // Creating an ArrayList of Object type
        ArrayList<Object> arr = new ArrayList<Object>();
        // Inserting String value in arr
        //  Inserting Integer value in arr
        //  Inserting Long value in arr
        // Inserting Double value in arr
        //  Inserting Float value in arr
        // arr after all insertions: ["GeeksForGeeks", 14,
        // 1800L, 6.0D, 1.99F]
            "ArrayList after all insertions: ");
        // Replacing element at index 0 (i.e. an String)
        // with an Integer type value 50
        arr.set(0, 50);
        // Replacing element at index 1 (Integer value)
        // with a Double type value
        arr.set(1, 10.0D);
        // arr after modifications: [50, 10.0D,
        // 1800L, 6.0D, 1.99F]
        System.out.print("ArrayList after modifications: ");
    // Function to display elements of the ArrayList
    public static void display(ArrayList<Object> arr)
        for (int i = 0; i < arr.size(); i++) {
            System.out.print(arr.get(i) + " ");



ArrayList after all insertions: GeeksForGeeks 14 1800 6.0 1.99 
ArrayList after modifications: 50 10.0 1800 6.0 1.99


My Personal Notes arrow_drop_up
Last Updated : 22 Oct, 2021
Like Article
Save Article
Similar Reads
Related Tutorials