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

时间:2014-12-11 21:17:02

标签: 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 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);

7 个答案:

答案 0 :(得分:3)

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

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 :(得分:2)

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

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<String[]> x

而不是

ArrayList<String[]> x

答案 2 :(得分:2)

试试这个:

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

答案 3 :(得分:2)

您可以执行String[][] finalArray = new String[][] {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"

答案 4 :(得分:2)

String的数组放到String[][]

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

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

你可以采用更短的方式(我建议):

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

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

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

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

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

根据Java Language Specification 10.3 Array Creation,较长的语法是 array creation expression ,较短的语法是 array initializer 。他们并不等同。 (如果是这样,设计师可能会摆脱其中一个 - Ockham's razor。)example可以使用数组创建表达式,但不能使用数组初始化


String的数组放到ArrayList<String[]>

最接近你尝试的方式:

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

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

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

并且,如果您将array1array2array3array4声明为final引用,则可以使用double brace initialization

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

答案 5 :(得分:1)

您可以执行以下操作

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,因此它可以混合静态和动态初始化,选择适合您需求的内容

答案 6 :(得分:0)

将数组添加到列表中:

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

将数组添加到2D数组:

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

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

变化:

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

收件人([]):

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

或直接声明数组:

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

其他阵列也一样。