Java 如何将字符串数组放入字符串数组数组中?
我有如下4个字符串数组:Java 如何将字符串数组放入字符串数组数组中?,java,arrays,Java,Arrays,我有如下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"}; Array myA
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"};
Array myArray = [{"there", "there2", "there3", "there4"},
{"here","here2","here3"},{"hi","hi2"},
{"blah","blah2","blah3"}];
myArray[0][1] would equal "there2"
myArray[1][2] would equal "here3"
我想把它们放到一个数组中,看起来像这样:
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"};
Array myArray = [{"there", "there2", "there3", "there4"},
{"here","here2","here3"},{"hi","hi2"},
{"blah","blah2","blah3"}];
myArray[0][1] would equal "there2"
myArray[1][2] would equal "here3"
然后可以访问其中的元素,有点像这样:
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"};
Array myArray = [{"there", "there2", "there3", "there4"},
{"here","here2","here3"},{"hi","hi2"},
{"blah","blah2","blah3"}];
myArray[0][1] would equal "there2"
myArray[1][2] would equal "here3"
希望这是有意义的,我怎么能这样做呢
我试着制作一个这样的ArrayList,然后添加它们,但似乎不起作用
ArrayList<String[]> myArrayList = new ArrayList<String[]>();
myArrayList.add(myArray);
ArrayList myArrayList=new ArrayList();
添加(myArray);
您可以执行以下操作
String[][] myArray = new String[][]{{"there", "there2", "there3", "there4"},
{"here","here2","here3"},{"hi","hi2"},
{"blah","blah2","blah3"}};
就像你说的那样
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
}
}
请注意,这与
String[][] myArray = new String[][] {
array1,
array2,
array3,
array4,
};
在第二种方法中,对例如
array1
的更改也将应用于myArray
,因此它是静态和动态初始化的混合,请选择适合您需要的内容我更正了您的定义,使其实际编译:
String[] array1 = {"there", "there", "there", "there"};
String[] array2 = {"here","here","here"};
String[] array3 = {"hi","hi"};
String[] array4 = {"blah","blah","blah"};
将数组添加到列表的peferred方法是内置方法,因为对数组的更改将反映在列表中
List<String[]> y = Arrays.asList(array1, array2, array3,array4);
List y=Arrays.asList(array1、array2、array3、array4);
注意:如果您定义了一个变量,即
List<String[]> x
列表x
而不是
ArrayList<String[]> x
ArrayList x
将数组添加到列表中:
List<String[]> list = Arrays.asList(array1, array2, array3, array4);
此外,您没有正确初始化阵列。按照您的方式,您正在调用字符串
对象的构造函数,而不是初始化数组,因此编译器将给您一个错误
更改:
String[] array1 = new String{"there", "there2", "there3", "there4"};
添加到(添加[]
):
或者直接声明数组,如下所示:
String[] array1 = {"there", "there2", "there3", "there4"};
其他阵列也是如此。尝试以下方法:
String[][] arraysTogether = new String[][]{
array1,
array2,
array3,
array4
};
您只需执行
String[][]finalArray=newstring[][]{array1,array2,array3,array4}代码>
如下
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"};
String[][] myArray= new String[][] {array1, array2, array3, array4};
System.out.println(myArray[2][1]);
这个打印“hi2”
如果你这样做
myArray[0][1] --> It would be "there2"
myArray[1][2] --> It would be "here3"
将String
数组放入String[][]
首先,String[]array1=新字符串{“there”,“there2”,“there3”,“there4”}
将不会编译。你可能在想:
String[] array1 = new String[]{"there", "there2", "there3", "there4"};
你可以用更短的方法(我会推荐):
现在,你的问题的答案是:
String[][] arrays = new String[][]{array1, array2, array3, array4};
或者,再次-更短(推荐):
根据,较长的语法是an,较短的语法是an。它们不是等价物。(如果是的话,设计者可能会去掉其中一个-)一个可以使用数组创建表达式,但不能使用数组初始值设定项的地方
将字符串的数组
放入数组列表
最接近您尝试的方式:
List<String[]> myArrayList = new ArrayList<String[]>();
myArrayList.add(array1);
myArrayList.add(array2);
myArrayList.add(array3);
myArrayList.add(array4);
并且,如果将array1
、array2
、array3
、array4
声明为最终
引用,则可以使用:
List myArrayList=new ArrayList(){{
添加(第1列);
添加(array2);
添加(第3列);
添加(第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"};
String[][] allArray = {
array1,
array2,
array3,
array4
};
这很奇怪。。。您已经知道如何访问所需的数组,但您还没有尝试以这种方式定义它?似乎您有输入错误<代码>字符串[]数组1=新字符串{“there”,“there2”,“there3”,“there4”}代码>将不会创建数组。要么删除新字符串[]
只留下字符串[]数组1={your,elements}
或通过添加[]
等新字符串[]{“there”、“there2”、“there3”、“there4}显式添加数组类型代码>。干得好,请从现在开始,然后像这样张贴您未来的答案,以便对其他可能看到您答案的人有所帮助。