bigkat79
bigkat79

Reputation: 1

Created a nested ArrayList from a string

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!

Upvotes: 0

Views: 130

Answers (2)

Fabian Damken
Fabian Damken

Reputation: 1517

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;
}

Upvotes: 0

user5156016
user5156016

Reputation:

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.

Upvotes: 1

Related Questions