Skip to content
Related Articles

Related Articles

Creating an ArrayList with Multiple Object Types in Java
  • Last Updated : 07 Jan, 2021

ArrayList class Java is basically a resizeable 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




// 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
        arr.add("GeeksForGeeks");
  
        //  Inserting Integer value in arr
        arr.add(14);
  
        //  Inserting Long value in arr
        arr.add(1800L);
  
        // Inserting Double value in arr
        arr.add(6.0D);
  
        //  Inserting Float value in arr
        arr.add(1.99F);
  
        // arr after all insertions: ["GeeksForGeeks", 14,
        // 1800L, 6.0D, 1.99F]
  
        System.out.print(
            "ArrayList after all insertions: ");
        display(arr);
  
        // 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: ");
  
        display(arr);
    }
  
    // 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) + " ");
        }
        System.out.println();
    }
}
Output
ArrayList after all insertions: GeeksForGeeks 14 1800 6.0 1.99 
ArrayList after modifications: 50 10.0 1800 6.0 1.99

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :