[英]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.