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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ranjan Kumar
  • 93
  • 2
  • 10
  • Have a look at [Is there a java equivalent of the python eval function?](http://stackoverflow.com/questions/7143343/is-there-a-java-equivalent-of-the-python-eval-function) and [Is there an eval() function in Java?](http://stackoverflow.com/questions/2605032/is-there-an-eval-function-in-java) – Sasha May 24 '16 at 10:08
  • use `split(delimiter)` method of **String** class. – Cataclysm May 24 '16 at 10:10
  • @Sasha.. i already saw that post but didn't find much help there .. – Ranjan Kumar May 24 '16 at 10:16
  • @Cataclysm .. Using delimiter here is not something i am looking for here .. – Ranjan Kumar May 24 '16 at 10:17

1 Answers1

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();
        }
    }
Poh Zi How
  • 1,489
  • 3
  • 16
  • 38
  • 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 ?? – Ranjan Kumar May 24 '16 at 10:29
  • 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. – Pradhan May 24 '16 at 10:57