1

I have a string something like:

String someString=[[((2016, 5, 21, 2, 0, 0), (2016, 5, 23, 7, 0, 0)), [('TASK1', 'DONE'), ('TASK2', 'DONE'), ('TASK3', 'DONE'), ('TASK4', 'DONE'), ('TASK5', 'DONE')]]]

I want to use this string to initialize a multidimensional array so that i can retrieve the values using indexes:

array[0] should give ((2016, 5, 21, 2, 0, 0), (2016, 5, 23, 7, 0, 0)) array[0][1] should give (2016, 5, 23, 7, 0, 0) and so on ..

In python, I was able to do it easily using the eval function but not sure how to do it in Java.

4
  • Have a look at Is there a java equivalent of the python eval function? and Is there an eval() function in Java? Commented May 24, 2016 at 10:08
  • use split(delimiter) method of String class. Commented May 24, 2016 at 10:10
  • @Sasha.. i already saw that post but didn't find much help there .. Commented May 24, 2016 at 10:16
  • @Cataclysm .. Using delimiter here is not something i am looking for here .. Commented May 24, 2016 at 10:17

1 Answer 1

0

What you are looking for seems to be a 3-dimensional array. Declare the array by adding three square brackets, and use curly braces to signify the end of each array. For example:

String someString [][][] = {
            {{"2016", "5", "21", "2", "0", "0"},{"2016", "5", "23", "7", "0", "0"}},
            {{"TASK1", "DONE"}, {"TASK2", "DONE"}}
            };

In this case, someString[0][0][0] will be 2016, and someString[1][1][0] will be TASK2, etc.

Displaying an array in java is slightly more complicated than python, where the standard way is to use multiple for loops to go through each array. For example:

        for (int h=0; h<someString.length; h++){
        for (int i=0; i<someString[h].length; i++){
            System.out.print(someString[h][i][0] + "\t");
            for (int j=0; j<someString[h][i].length; j++){
                System.out.print(someString[h][i][j]+"\t");
            }
            System.out.println();
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

This example will work when i can declare my own string array but i am getting a string instead of string array so how do i convert that string to the 3D string array you mentioned ??
In that case you will have to write code to parse the string and create multi-dimensional arrays yourself. There is no direct way of doing this. Post the exact format of the string or a couple of sample strings. May be we can come up with a simple algo.

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.