Tutorial

Java Array of ArrayList, ArrayList of Array

Published on August 4, 2022
author

Pankaj

Java Array of ArrayList, ArrayList of Array

Today we will learn how to create a Java array of ArrayList. We will also learn how to create an ArrayList of array elements.

Java Array of ArrayList

java array of arraylist, array of lists in java Creating array of list in java is not complex. Below is a simple program showing java Array of ArrayList example.

import java.util.ArrayList;
import java.util.List;

public class JavaArrayOfArrayList {

	public static void main(String[] args) {
		List<String> l1 = new ArrayList<>();
		l1.add("1");
		l1.add("2");

		List<String> l2 = new ArrayList<>();
		l2.add("3");
		l2.add("4");
		l2.add("5");

		List<String>[] arrayOfList = new List[2];
		arrayOfList[0] = l1;
		arrayOfList[1] = l2;

		for (int i = 0; i < arrayOfList.length; i++) {
			List<String> l = arrayOfList[i];
			System.out.println(l);
		}

	}

}

Notice that we can’t use generics while creating the array because java doesn’t support generic array. So if we try to use below code, it will produce compile time error as “Cannot create a generic array of List<String>”.

List<String>[] arrayOfList = new List<String>[2];

Java ArrayList of Array

We can also create an array whose elements are a list. Below is a simple example showing how to create a list of array elements in java.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class JavaArrayListOfStringArray {

	public static void main(String[] args) {
		// List of String arrays
		List<String[]> list = new ArrayList<String[]>();
		
		String[] arr1 = { "a", "b", "c" };
		String[] arr2 = { "1", "2", "3", "4" };
		list.add(arr1);
		list.add(arr2);
		// printing list of String arrays in the ArrayList
		for (String[] strArr : list) {
			System.out.println(Arrays.toString(strArr));
		}
	}

}

java arraylist of array

Java ArrayList of Object Array

If you are not sure about the type of objects in the array or you want to create an ArrayList of arrays that can hold multiple types, then you can create an ArrayList of an object array. Below is a simple example showing how to create ArrayList of object arrays in java.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class JavaArrayListOfObjectArray {

	public static void main(String[] args) {
		// list of Object arrays to hold different types of array
		List<Object[]> list = new ArrayList<Object[]>();
		String[] arr1 = { "a", "b", "c" };
		String[] arr2 = { "1", "2", "3", "4" };

		JavaArrayListOfObjectArray aa = new JavaArrayListOfObjectArray();
		JavaArrayListOfObjectArray.A[] arr3 = { aa.new A("AA"), aa.new A("BB") };

		list.add(arr1);
		list.add(arr2);
		list.add(arr3);

		// list holds different types of Object arrays, let's print them
		for (Object[] objArr : list) {
			System.out.println(Arrays.toString(objArr));

			// iterating over the array and doing operation based on it's type
			for (Object obj : objArr) {

				// using instanceof keyword to find the Object type in the array
				if (obj instanceof String) {
					// doSomethingForStringObject();
				} else if (obj instanceof JavaArrayListOfObjectArray.A) {
					// doSomethingForAObject();
				}
			}
		}
	}

	/**
	 * A sample class to use in arraylist of arrays
	 * 
	 * @author pankaj
	 * 
	 */
	public class A {
		private String name;

		public A(String n) {
			this.name = n;
		}

		public String getName() {
			return this.name;
		}

		@Override
		public String toString() {
			return "A.class::"+this.name;
		}
	}
}

When we execute the above program, it produces the following output.

[a, b, c]
[1, 2, 3, 4]
[A.class::AA, A.class::BB]

That’s all for creating an Array of ArrayList and ArrayList of Array in Java.

You can checkout more core java examples from our GitHub Repository.

Reference: Java Arrays

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
March 18, 2021

On the space below, write down the code for declaring an integer array whare the values are 1,4,3,5and 6.

- Narzde Sam

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    March 26, 2019

    Can someone help me out with simple coding with ARRAYS

    - Eldridge Gee

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      May 4, 2018

      how i can remove specific value on list of array string?

      - Annisa Nurika

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        November 8, 2016

        Thank you very much Pankaj this example is very clear and very helpful.

        - Lithi P

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          October 29, 2015

          Dear Pankaj, Very usefull your article. As I have told you the last time in my email to you. With this article, I have an idea developped below: - Create an arraylist: ArrayList dico = new ArrayList(); String[2] is a table of two String elements which represents ‘value of word attribute’ and 'value of the word"s definition". -But i don’t know how to populate my list ‘dico’. Please give me your solution. I wait your response. Thank in advance. Vu

          - vu

            Try DigitalOcean for free

            Click below to sign up and get $200 of credit to try our products over 60 days!

            Sign up

            Join the Tech Talk
            Success! Thank you! Please check your email for further details.

            Please complete your information!

            Become a contributor for community

            Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

            DigitalOcean Documentation

            Full documentation for every DigitalOcean product.

            Resources for startups and SMBs

            The Wave has everything you need to know about building a business, from raising funding to marketing your product.

            Get our newsletter

            Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

            New accounts only. By submitting your email you agree to our Privacy Policy

            The developer cloud

            Scale up as you grow — whether you're running one virtual machine or ten thousand.

            Get started for free

            Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

            *This promotional offer applies to new accounts only.