我有4个这样的字符串数组:
String[] array1 = new String{"there", "there2", "there3", "there4"};
String[] array2 = new String{"here","here2","here3"};
String[] array3 = new String{"hi","hi2"};
String[] array4 = new String{"blah","blah2","blah3"};
Run Code Online (Sandbox Code Playgroud)
我想将这些放入一个数组数组,看起来像这样:
Array myArray = [{"there", "there2", "there3", "there4"},
{"here","here2","here3"},{"hi","hi2"},
{"blah","blah2","blah3"}];
Run Code Online (Sandbox Code Playgroud)
然后就可以像这样访问它中的元素:
myArray[0][1] would equal "there2"
myArray[1][2] would equal "here3"
Run Code Online (Sandbox Code Playgroud)
希望有道理,我怎么能这样做呢?
我试过像这样制作一个ArrayList然后添加它们但它似乎不起作用
ArrayList<String[]> myArrayList = new ArrayList<String[]>();
myArrayList.add(myArray);
Run Code Online (Sandbox Code Playgroud)
您可以执行以下操作
String[][] myArray = new String[][]{{"there", "there2", "there3", "there4"},
{"here","here2","here3"},{"hi","hi2"},
{"blah","blah2","blah3"}};
Run Code Online (Sandbox Code Playgroud)
并按照您所说的方式访问
public class Test
{
public static void main(String[] args) {
String[][] myArray = new String[][]{{"there", "there2", "there3", "there4"},
{"here","here2","here3"},{"hi","hi2"},
{"blah","blah2","blah3"}};
System.out.println(myArray[0][1]); // there2
System.out.println(myArray[1][2]); // here3
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,这与 to 是有区别的
String[][] myArray = new String[][] {
array1,
array2,
array3,
array4,
};
Run Code Online (Sandbox Code Playgroud)
因为在第二种方法中,对eg的更改也array1将适用myArray,因此它是静态和动态初始化的混合,请选择适合您需要的内容
| 归档时间: |
|
| 查看次数: |
637 次 |
| 最近记录: |