简体   繁体   English

使用java中的内置字符串初始化多维数组

[英]initialize a multidimentional array using inbuilt string in java

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 .. array[0]应该给((2016, 5, 21, 2, 0, 0), (2016, 5, 23, 7, 0, 0)) array[0][1]应该给(2016, 5, 23, 7, 0, 0)等等..

In python, I was able to do it easily using the eval function but not sure how to do it in Java. 在python中,我能够使用eval函数轻松完成,但不知道如何在Java中完成它。

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. 在这种情况下,someString [0] [0] [0]将是2016年,someString [1] [1] [0]将是TASK2等。

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. 在java中显示数组比python稍微复杂一些,其中标准方法是使用多个for循环来遍历每个数组。 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();
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 A* Path Finder (Java) 使用 1024 多维数组效率低下 - A* Path Finder (Java) is inefficient using a 1024 multidimentional array 使用 RegEx 在 java 中使用内置字符串 function 更改日期 - Using RegEx to change the date with string inbuilt function in java 初始化未知大小的多维/锯齿状字符串数组 - Initializing Multidimentional/Jagged String Array of Unknown Size Java:如何移动多维数组的内容? - Java: how to move the contents of a multidimentional array? 如何在不使用 String Builder 和 String Buffer 等内置函数的情况下反转字符串数组 - How to reverse String Array without using inbuilt function like String Builder and String Buffer Java从一个字符串数组初始化多个字符串 - java initialize multiple Strings from a String array 我们如何在不使用任何内置函数的情况下找到Java中String的长度? (甚至不包括charAt()或toCharArray()或length()) - How can we find the length of a String in Java without using any inbuilt functions? (not even charAt() or toCharArray() or length()) Java使用字符串和反射初始化对象 - Java initialize object using string and reflection 有没有办法找到字符串长度而无需在Java中使用任何内置功能? - Is there any way to find string lenght without using any inbuilt fuctions in java? 是否可以在不使用 Java 内置数组、列表等的情况下将相同类型的对象存储到另一个类? - Can you store objects of the same type to another class without using the java inbuilt array, list, etc.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM