0

I am looking to created an ArrayList that is the exact replica of a String representation of a nested list. So given "[4, 9, 12, [1,2,3], [5,6,10], [11,12]]" the list would be [4,9,12,[1,2,3],[5,6,10],[11,12]].

static int position =0;

public static ArrayList stringToList(String input) {
       List<ArrayList> parsedList = new ArrayList<>();

        while(position < input.length()){
            char element = input.charAt(position++);


            if(element == '['){
                parsedList.add(parseListsToString(input));
            }else if(element==']'){
                break;
            }else if(element==','){}
            else{
                parsedList.add(element);
            }

        }

        return parsedList;
    }

I have tried declaring parsedList as: ArrayList(ArrayList) (which allows for recursion) and ArrayList(Integer) (which doesn't allow for recursion). And in the current code that I submitted about is obviously wrong because parsedList is an incorrect return type because its not an ArrayList.

I believe that my method in going about the problem is right just that my understanding of ArrayList is lacking and that is where I need help. So any suggestions would be appreciated! Thanks in advance!

8
  • 2
    Is this even possible? What type of ArrayList can hold a both an Integer and a List? Commented Oct 16, 2015 at 20:57
  • 2
    something like ArrayList<ArrayList<Integer>>. It is very possible. Commented Oct 16, 2015 at 21:03
  • 1
    you are missing ] after the first '12'. In your testing String Commented Oct 16, 2015 at 21:05
  • I have tried the ArrayList<ArrayList<Integer>> method and I am unable to add the elements to it because they are not of type ArrayList. And there shouldn't be a ']' after the first 12 as it is part of the outer most array. [4,9,12, [nested], [nested], [nested]], unless you think that's wrong. Do I need to manipulate the element in someway to add it if I define it as ArrayList<ArrayList<Integer>>? Commented Oct 16, 2015 at 21:10
  • I will edit my answer Commented Oct 16, 2015 at 21:14

2 Answers 2

1

Hi I debbuged and tested this solution.

String input = "[4, 9, 12, [1,2,3], 2, [5,6,10], [11,12],2,4 ,[5,6]]"; 
List<ArrayList<Integer>> parsedList = new ArrayList<ArrayList<Integer>>();      
    int count = -1;
    boolean newArray = true;

    String element = "";
    for(int i = 1; i < input.length()-1; i++){

        char temp = input.charAt(i); 
        element += temp;

        if(temp == '['){
            parsedList.add(new ArrayList<Integer>());
            count++;
            element = "";
            newArray = false;
        }
        else if(temp == ']'){ 
            parsedList.get(count).add(Integer.parseInt(element.replaceAll("]","")));                    element ="";                
            newArray = true;

        }
        else if(temp == ',' && element.length() == 1 ){

             element = "";  

        }
        else if(temp == ',' && element.length() != 1){

            if(newArray){
                parsedList.add(new ArrayList<Integer>());
                count++;                    
            }

            parsedList.get(count).add(Integer.parseInt(element.replaceAll(",","").replaceAll(" ", "")));
            element ="";

        }

    }

The result is [[4],[9],[12],[1,2,3],[2],[5,6,10],[11,12],[2],[4],[5,6]]. So basically yo have an array of integer arrays.

Sign up to request clarification or add additional context in comments.

3 Comments

It will not work for multi-dimensional arrays... Also, why do you have every number in a single List?
Well it answers the question.
Well, yes. @bigkat79 has posted this question again this day. With the same name. The question there was a bit different, but really a bit. But he has deleted it...
0

I posted my solution previously, but it disappeared:

private static List<Object> toArray(final String str) {
    return Main.toArray(new StringBuilder(str));
}

private static List<Object> toArray(final StringBuilder data) {
    final List<Object> result = new ArrayList<Object>();

    if (data.charAt(0) == '[') {
        data.delete(0, 1);
    }

    String value = "";
    while (data.length() > 0) {
        final char element = data.charAt(0);
        data.delete(0, 1);

        if (element == '[') {
            result.add(Main.toArray(data));
        } else if (element == ']') {
            if (!value.isEmpty()) {
                result.add(value);
                value = "";
            }
            break;
        } else if (element == ',') {
            if (!value.isEmpty()) {
                result.add(value);
                value = "";
            }
        } else if (element != ' ') {
            value += element;
        }
    }

    if (!value.isEmpty()) {
        result.add(value);
    }

    return result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.