0

I have a nested arraylist, for example:

[[[1,2],[4,3]],[[5,6],[7,8]]]

I want to convert it into a simple arraylist, so it becomes

[1,2,4,3,5,6,7,8]

Is there a way to do this?

Here is my code:

public static Object toArrayList(ArrayList nested){
    boolean bool = false;
    while(!bool){
        bool = nested instanceof ArrayList;
        ArrayList tmp = new ArrayList();
        for(Object o : nested){
            if(o instanceof Integer){
                System.out.println(o);
                tmp.add(o);
            }else{
                tmp.add(toArrayList((ArrayList)o));
            }
        }
        nested = tmp;
    }
    return nested;
}

However, when I run it, it doesn't convert it, just returns the same thing.

UnsignedByte
  • 849
  • 10
  • 29
  • 2
    I wonder why you didn't try anything but post it here! Show us some code that, you've worked on. – Naman Jan 28 '17 at 17:08
  • Welcome to Stack Overflow! It looks like you are asking for homework help. While we have no issues with that per se, please observe these [dos and don'ts](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions/338845#338845), and edit your question accordingly. – Joe C Jan 28 '17 at 17:17
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. – Naman Jan 28 '17 at 17:19

0 Answers0