Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 519799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:04:42+00:00 2026-05-13T08:04:42+00:00

In Java I want to convert a nested List which contains at the deepest

  • 0

In Java I want to convert a nested List which contains at the deepest level a uniform type into an multidimensional array of that type. For example, ArrayList<ArrayList<ArrayList<ArrayList<String>>>> into String[][][][]. I’ve tried several things and I only can obtain an array of objects like Object[][][][]. For ‘simple lists’ it seems that Apache Commons Lang does the work but I cannot figure out for nested cases.

Update:

In order to obtain a multidimensional array of Object type I’m using a recursive function so I cannot set the key type using toArray() see excerpt:

// the argument of this function is a (nested) list
public static Object convert(Object object) {

    Object[] result = null;
    List list = (List) object;
    if (list != null) {

        Object type = getElementType(list);
        if (type instanceof List) {

            int size = list.size();
            result = new Object[size];
            for (int counter = 0; counter < size; counter++) {

                Object element = list.get(counter);
                result[counter] = (element != null) ? convert(element) : null;
            }
        } else {
            result = list.toArray();
        }
    }

    return result;
}

private static Object getElementType(List list) {

    Object result = null;
    for (Object element : list) {
        if (element != null) {

            result = element;
            break;
        }
    }

    return result;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:04:43+00:00Added an answer on May 13, 2026 at 8:04 am

    This is the way that someone suggested to solved for String type. Cast2(List<?>) returns the multidimensional array. It may be generalized to use the class type as parameter. Thank you for your comments.

    static int dimension2(Object object) {
    
        int result = 0;
        if (object instanceof List<?>) {
    
            result++;
            List<?> list = (List<?>) object;
            for (Object element : list) {
                if (element != null) {
                    result += dimension2(element);
                    break;
                }
            }
        }
    
        return result;
    }
    
    
    static Object cast2(List<?> l) {
    
        int dim = dimension2(l);
        if (dim == 1) {
            return l.toArray(new String[0]);
        }
    
        int[] dims = new int[dimension2(l)];
        dims[0] = l.size();
        Object a = Array.newInstance(String.class, dims);
        for (int i = 0; i < l.size(); i++) {
    
            List<?> e = (List<?>) l.get(i);
            if (e == null) {
                Array.set(a, i, null);
            } else if (dimension2(e) > 1) {
                Array.set(a, i, cast2(e));
            } else {
                Array.set(a, i, e.toArray(new String[0]));
            }
        }
        return a;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert a java string that contains UTF-8 characters to a format
I want to convert XML into binary data in Java? What is the fastest
I want to convert string Date into Timestamp in java. The following coding i
I just installed Jython 2.5.1. I want to convert my Python file into Java
I want to convert a data string into a timestamp (which would include the
I have file contents in a java string variable, which I want to convert
I want to convert a List to a List so that each object on
I have a float array in java and want to convert each element to
I have a large XML document that I want to convert to a Java
My java project build in jdk 7 . I want to convert it into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.