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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 322k
  • Answers 322k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you are using Apache, simple do (on htaccess): RewriteEngine… May 14, 2026 at 12:47 am
  • Editorial Team
    Editorial Team added an answer apply(df, 1, paste, collapse="") May 14, 2026 at 12:47 am
  • Editorial Team
    Editorial Team added an answer According to Koenig Lookup (C++ Standard 3.4.2) operator<< will be… May 14, 2026 at 12:47 am

Related Questions

How to convert a string 0E-11 to 0.00000000000 in Java? I want to display
In Java, I have a String and I want to encode it as a
In C# if I want to convert a double (1.71472) to an int then
Does anyone know how to convert a string from ISO-8859-1 to UTF-8 and back
I have a number of different representations of the same kind of object; let's

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.