【问题标题】:Converting List<List<String>> to array将 List<List<String>> 转换为数组
【发布时间】:2016-10-31 07:27:25
【问题描述】:

我有在列表变量中声明的元素,例如:

List<List<String>> textList = new ArrayList<>();

元素添加如:

textList.add(Arrays.asList(p)); //adding elements

我可以输出变量内的元素的唯一方法是使用:

 for(List<String>  s: textList){
      System.out.println(s); }

哪些输出元素是这样的:

[He is a boy.]
[He likes apple.]
[She is a girl.]

现在,我想将它们存储在一个 数组 中,以便输出时元素看起来像这样。

[He is a boy., He likes apple., She is a girl.]

我试过了

String[] textArr = new String[textList.size()];
textArr = textList.toArray(textArr);

for(String s : textArr){
    System.out.println(s);}

但我收到一个错误:

Exception in thread "main" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.Arrays.copyOf(Arrays.java:3213)
at java.util.ArrayList.toArray(ArrayList.java:407)

那么,如何使用正确的方式将列表中的元素转换为数组。谢谢!

【问题讨论】:

标签: java arrays list


【解决方案1】:

您的问题是您没有在列表 textList 中存储字符串。

textList.add(Arrays.asList(p));

正如类型所说,这里有一个字符串列表。

因此,您不能将该列表中的元素假定为字符串。因为他们不是!错误消息告诉你: toArray() 想要它可以放入该字符串数组中的字符串,但你给它一个 List of String !

但问题是:你在这里描述的首先没有意义。 打印字符串不应该关心字符串是在数组还是列表中。

我的意思是:当您手动迭代 List 或数组以打印其内容时,迭代 List 或数组绝对无关紧要。代码也是一样的:

for (String someString : someCollection) { 
  System.out.println(someString);
}

someCollection 可以同时是:数组或列表!

换句话说:将很好存储在列表中的数据转换为数组以打印的想法根本没有任何意义。相反:您可能在您的 List 对象上调用 toString() ,结果...不是您想要的 100%。但我向你保证:在某个数组上调用 toString() 会导致你完全不想要的东西。

长话短说:忘记转换为数组;只需迭代您的 List of Strings 并使用 StringBuilder 以您希望的方式收集该集合的内容(您只需将这些 [ ] 字符附加到该构建器的那些您希望他们看到的地方)。

(如果您坚持要转换为数组,那么要理解的关键点是,只有一个字符串列表才能转换为字符串数组。所以一个列表列表......并不那么容易)。

【讨论】:

    【解决方案2】:

    使用流和flatMap,您可以这样做:

    List<List<String>> list = ...;
    String[] strings = list.stream().flatMap(l -> l.stream()).collect(Collectors.toList()).toArray(new String[0]);
    

    这等效于使用循环(您可以按照 cmets 中的建议使用两个嵌套的 for 循环来代替 addAll,但为什么呢?):

    List<List<String>> list = ...;
    List<String> stringList = new ArrayList<>();
    for (List<String> l : list)
      stringList.addAll(l);
    String[] strings = list.toArray(new String[stringList.size()]);
    

    【讨论】:

      【解决方案3】:

      您可以使用迭代器来遍历列表的每个元素,for each 语句的实例(我个人更喜欢迭代器)。您可以使用的代码类似于

               //Your list
          List<List<String>> textList = new ArrayList<>();
      
          //The iterators
          Iterator<List<String>> itList = textList.iterator();
          Iterator<String> itString;
      
          //The string to store the phrases
          String s[] = new String[textList.size()];
      
          int i =0;
      
          //First loop, this seeks on every list of lists
          while(itList.hasNext()){
              //Getting the iterator of strings
              itString = itList.next().iterator();
      
              s[i] = "";
              //2nd loop, it seeks on every List of string
              while(itString.hasNext()){
                 s[i] = s[i].concat(itString.next());
              }
              s[i] = s[i].concat(".");
              i++;
          }
      

      【讨论】:

        猜你喜欢
        • 2021-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-28
        • 1970-01-01
        • 2021-01-13
        • 1970-01-01
        相关资源
        最近更新 更多