如何将String数组放入String数组数组?

时间:2021-12-28 20:21:22

I have 4 string arrays like this:

我有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"};

And I want to put these into an array of arrays that would look something like this:

我想把它们放到一个看起来像这样的数组数组中:

   Array myArray =  [{"there",  "there2", "there3", "there4"},
                     {"here","here2","here3"},{"hi","hi2"},
                     {"blah","blah2","blah3"}];

And then would be able to access element in it somewhat like this:

然后就可以像这样访问元素:

myArray[0][1] would equal "there2"
myArray[1][2] would equal "here3"

Hope that makes sense, how could I go about doing this?

希望有道理,我怎么能这样做呢?

I have tried making an ArrayList like this and then adding them but it doesn't seem to work

我试过像这样制作一个ArrayList然后添加它们但它似乎不起作用

ArrayList<String[]> myArrayList = new ArrayList<String[]>();
myArrayList.add(myArray);

7 个解决方案

#1


3  

It's simple. Check this link for more information about Creating, Initializing, and Accessing an Array.

这很简单。有关创建,初始化和访问阵列的更多信息,请查看此链接。

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
        };

#2


2  

Try this:

String[][] arraysTogether = new String[][]{
    array1,
    array2,
    array3,
    array4
};

#3


2  

You can simply do String[][] finalArray = new String[][] {array1, array2, array3, array4};

你可以简单地做String [] [] finalArray = new String [] [] {array1,array2,array3,array4};

as below

    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]);

This prints "hi2"

这打印“hi2”

if you do

如果你这样做

        myArray[0][1] --> It would be "there2"
        myArray[1][2] --> It would be "here3"

#4


2  

I corrected your definition so it actually compiles:

我更正了你的定义,所以它实际编译:

String[] array1  = {"there",  "there", "there", "there"};
String[] array2  = {"here","here","here"};
String[] array3  = {"hi","hi"};
String[] array4  = {"blah","blah","blah"};

The peferred method to add the to a list is the built in one, as changes to the array will be reflected by the list.

添加到列表的peferred方法是内置的,因为数组的更改将由列表反映。

List<String[]> y = Arrays.asList(array1, array2, array3,array4);

one sidenote: always prfere to use the Interface if you define a variable i.e.

一个旁注:如果你定义一个变量,总是会使用接口,即

List<String[]> x

instead of

ArrayList<String[]> x

#5


2  

Putting arrays of String to String[][]

First of all, String[] array1 = new String{"there", "there2", "there3", "there4"} will not compile. You were probably thinking about:

首先,String [] array1 = new String {“there”,“there2”,“there3”,“there4”}将无法编译。你可能在考虑:

String[] array1 = new String[]{"there", "there2", "there3", "there4"};

You can do it shorter way (which I will recommend):

你可以用更短的方式(我会推荐):

String[] array1 = {"there", "there2", "there3", "there4"};

Now, the answer to your question is:

现在,您的问题的答案是:

String[][] arrays = new String[][]{array1, array2, array3, array4};

Or, again - shorter (and recommended):

或者,再次 - 更短(和推荐):

String[][] arrays = {array1, array2, array3, array4};

According to Java Language Specification 10.3 Array Creation, the longer syntax is an array creation expression and the shorter one is an array initializer. They're not equivalent. (If they were, the designers would probably get rid of one of them - Ockham's razor.) An example where one can use array creation expression, but not array initializer.

根据Java语言规范10.3阵列创建,较长的语法是数组创建表达式,较短的语法是数组初始化程序。他们不等同。 (如果是的话,设计师可能会摆脱其中一个--Ockham的剃刀。)一个例子,可以使用数组创建表达式,但不能使用数组初始化程序。


Putting arrays of String to ArrayList<String[]>

The closest one to the way you tried:

与您尝试的方式最接近的方式:

List<String[]> myArrayList = new ArrayList<String[]>();
myArrayList.add(array1);
myArrayList.add(array2);
myArrayList.add(array3);
myArrayList.add(array4);

The shortest one using Arrays.asList():

使用Arrays.asList()的最短的:

List<String[]> myArrayList = Arrays.asList(array1, array2, array3, array4);

And, if you declare array1, array2, array3, array4 as final references, you can use double brace initialization:

并且,如果将array1,array2,array3,array4声明为最终引用,则可以使用双括号初始化:

List<String[]> myArrayList = new ArrayList<String[]>() {{
    add(array1);
    add(array2);
    add(array3);
    add(array4);
}};

#6


1  

You can do as follows

你可以这样做

String[][] myArray =  new String[][]{{"there",  "there2", "there3", "there4"},  
                                     {"here","here2","here3"},{"hi","hi2"}, 
                                     {"blah","blah2","blah3"}};

and access just as you said

就像你说的那样访问

    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
    }
}

Note that there's a difference than to

请注意,与...不同

String[][] myArray = new String[][] {
    array1,
    array2,
    array3,
    array4,
};

in that that in the second approach, the changes to an e.g. array1 will apply to myArray as well, so its a mix of static and dynamic initialization, choose what suits your needs

因为在第二种方法中,对例如array1也将应用于myArray,因此它混合了静态和动态初始化,选择适合您需求的内容

#7


0  

Adding arrays into a list:

将数组添加到列表中:

List<String[]> list = Arrays.asList(array1, array2, array3, array4);

Adding arrays to a 2D Array:

将数组添加到2D数组:

String[][] array = new String[][] {
    array1,
    array2,
    array3,
    array4,
};

Also, you are not initializing your arrays properly. The way you have it, you are calling the constructor of the String object, not initializing an array so the compiler will give you an error.

此外,您没有正确初始化阵列。你有它的方式,你正在调用String对象的构造函数,而不是初始化一个数组,所以编译器会给你一个错误。

Change:

String[] array1  = new String{"there",  "there2", "there3", "there4"};

To (add []):

加上 []):

String[] array1  = new String[]{"there",  "there2", "there3", "there4"};

Or declare the array directly like:

或直接声明数组:

String[] array1  = {"there",  "there2", "there3", "there4"};

Same goes for the other arrays.

其他阵列也是如此。

#1


3  

It's simple. Check this link for more information about Creating, Initializing, and Accessing an Array.

这很简单。有关创建,初始化和访问阵列的更多信息,请查看此链接。

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
        };

#2


2  

Try this:

String[][] arraysTogether = new String[][]{
    array1,
    array2,
    array3,
    array4
};

#3


2  

You can simply do String[][] finalArray = new String[][] {array1, array2, array3, array4};

你可以简单地做String [] [] finalArray = new String [] [] {array1,array2,array3,array4};

as below

    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]);

This prints "hi2"

这打印“hi2”

if you do

如果你这样做

        myArray[0][1] --> It would be "there2"
        myArray[1][2] --> It would be "here3"

#4


2  

I corrected your definition so it actually compiles:

我更正了你的定义,所以它实际编译:

String[] array1  = {"there",  "there", "there", "there"};
String[] array2  = {"here","here","here"};
String[] array3  = {"hi","hi"};
String[] array4  = {"blah","blah","blah"};

The peferred method to add the to a list is the built in one, as changes to the array will be reflected by the list.

添加到列表的peferred方法是内置的,因为数组的更改将由列表反映。

List<String[]> y = Arrays.asList(array1, array2, array3,array4);

one sidenote: always prfere to use the Interface if you define a variable i.e.

一个旁注:如果你定义一个变量,总是会使用接口,即

List<String[]> x

instead of

ArrayList<String[]> x

#5


2  

Putting arrays of String to String[][]

First of all, String[] array1 = new String{"there", "there2", "there3", "there4"} will not compile. You were probably thinking about:

首先,String [] array1 = new String {“there”,“there2”,“there3”,“there4”}将无法编译。你可能在考虑:

String[] array1 = new String[]{"there", "there2", "there3", "there4"};

You can do it shorter way (which I will recommend):

你可以用更短的方式(我会推荐):

String[] array1 = {"there", "there2", "there3", "there4"};

Now, the answer to your question is:

现在,您的问题的答案是:

String[][] arrays = new String[][]{array1, array2, array3, array4};

Or, again - shorter (and recommended):

或者,再次 - 更短(和推荐):

String[][] arrays = {array1, array2, array3, array4};

According to Java Language Specification 10.3 Array Creation, the longer syntax is an array creation expression and the shorter one is an array initializer. They're not equivalent. (If they were, the designers would probably get rid of one of them - Ockham's razor.) An example where one can use array creation expression, but not array initializer.

根据Java语言规范10.3阵列创建,较长的语法是数组创建表达式,较短的语法是数组初始化程序。他们不等同。 (如果是的话,设计师可能会摆脱其中一个--Ockham的剃刀。)一个例子,可以使用数组创建表达式,但不能使用数组初始化程序。


Putting arrays of String to ArrayList<String[]>

The closest one to the way you tried:

与您尝试的方式最接近的方式:

List<String[]> myArrayList = new ArrayList<String[]>();
myArrayList.add(array1);
myArrayList.add(array2);
myArrayList.add(array3);
myArrayList.add(array4);

The shortest one using Arrays.asList():

使用Arrays.asList()的最短的:

List<String[]> myArrayList = Arrays.asList(array1, array2, array3, array4);

And, if you declare array1, array2, array3, array4 as final references, you can use double brace initialization:

并且,如果将array1,array2,array3,array4声明为最终引用,则可以使用双括号初始化:

List<String[]> myArrayList = new ArrayList<String[]>() {{
    add(array1);
    add(array2);
    add(array3);
    add(array4);
}};

#6


1  

You can do as follows

你可以这样做

String[][] myArray =  new String[][]{{"there",  "there2", "there3", "there4"},  
                                     {"here","here2","here3"},{"hi","hi2"}, 
                                     {"blah","blah2","blah3"}};

and access just as you said

就像你说的那样访问

    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
    }
}

Note that there's a difference than to

请注意,与...不同

String[][] myArray = new String[][] {
    array1,
    array2,
    array3,
    array4,
};

in that that in the second approach, the changes to an e.g. array1 will apply to myArray as well, so its a mix of static and dynamic initialization, choose what suits your needs

因为在第二种方法中,对例如array1也将应用于myArray,因此它混合了静态和动态初始化,选择适合您需求的内容

#7


0  

Adding arrays into a list:

将数组添加到列表中:

List<String[]> list = Arrays.asList(array1, array2, array3, array4);

Adding arrays to a 2D Array:

将数组添加到2D数组:

String[][] array = new String[][] {
    array1,
    array2,
    array3,
    array4,
};

Also, you are not initializing your arrays properly. The way you have it, you are calling the constructor of the String object, not initializing an array so the compiler will give you an error.

此外,您没有正确初始化阵列。你有它的方式,你正在调用String对象的构造函数,而不是初始化一个数组,所以编译器会给你一个错误。

Change:

String[] array1  = new String{"there",  "there2", "there3", "there4"};

To (add []):

加上 []):

String[] array1  = new String[]{"there",  "there2", "there3", "there4"};

Or declare the array directly like:

或直接声明数组:

String[] array1  = {"there",  "there2", "there3", "there4"};

Same goes for the other arrays.

其他阵列也是如此。