【问题标题】:How to put String arrays into an array of String arrays?如何将字符串数组放入字符串数组的数组中?
【发布时间】:2014-12-11 21:17:01
【问题描述】:

我有 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);

【问题讨论】:

  • 这很奇怪......你已经知道如何访问你想要的数组,但你还没有尝试过这样定义它?
  • 看来你打错了。 String[] array1 = new String{"there", "there2", "there3", "there4"}; 不会创建数组。要么删除new String[],只留下String[] array1 = {your, elemements};,要么通过添加[](如new String[]{"there", "there2", "there3", "there4"};)显式添加数组类型。

标签: java arrays


【解决方案1】:

这很简单。查看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
        };

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

      【解决方案3】:

      你可以简单地做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】:

        我更正了您的定义,因此它实际上可以编译:

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

        添加到列表的首选方法是内置方法,因为对数组的更改将反映在列表中。

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

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

        List<String[]> x
        

        而不是

        ArrayList<String[]> x
        

        【讨论】:

        • 干得好,请从现在开始,然后像这样发布您未来的答案,以便其他可能看到您的答案的人有所帮助。
        【解决方案5】:

        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&lt;String[]&gt;

        最接近您尝试的方式:

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

        【讨论】:

          【解决方案6】:

          你可以这样做

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

          注意和to有区别

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

          在第二种方法中,对例如的更改array1 也适用于 myArray,所以它混合了静态和动态初始化,选择适合您的需求

          【讨论】:

            【解决方案7】:

            将数组添加到列表中:

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

            将数组添加到二维数组:

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

            其他数组也是如此。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2013-09-22
              • 1970-01-01
              • 1970-01-01
              • 2013-12-25
              • 1970-01-01
              • 1970-01-01
              • 2021-12-13
              相关资源
              最近更新 更多