如何在Java中实例化List>?

huangapple go评论72阅读模式
英文:

How should I instantiate List<List<String>> in Java

问题

我有以下的代码:

            List<List<String>> allData = getData()

            if (allData == null)
                allData = new ArrayList<ArrayList<String>>();
            // 在下面填充 allData

现在我想要初始化 allData 但是我得到了 Type mismatch: cannot convert from ArrayList<ArrayList<String>> to List<List<String>> 错误。我应该如何正确地初始化它?

getData() 返回 ArrayList<ArrayList<String>> 是不可能的。

谢谢!

英文:

I have the following code:

            List&lt;List&lt;String&gt;&gt; allData= getData()
			
			if (allData== null)
				allData= new ArrayList&lt;ArrayList&lt;String&gt;&gt;();
            // populate allData below

Now I want to initialize allData but I get Type mismatch: cannot convert from ArrayList&lt;ArrayList&lt;String&gt;&gt; to List&lt;List&lt;String&gt;&gt;. What is the correct way I can initialize this?

It is not possible to return ArrayList&lt;ArrayList&lt;String&gt;&gt; from getData()

Thanks!

答案1

得分: 9

你可以很简单地这样做:

allData = new ArrayList<>();

然后你可以向allData中添加新的列表

```java
List innerList = new ArrayList<>();
innerList.add("一些字符串");
// .... 等等 ....
allData.add(innerList);
英文:

You do it very simply:

allData = new ArrayList&lt;&gt;();

Then you can add new lists to allData:

List innerList = new ArrayList&lt;&gt;();
innerList.add(&quot;some string&quot;);
// .... etc ...
allData.add(innerList);

答案2

得分: 3

你不能在实例化具体实现时重新定义引用的泛型类型。引用是List&lt;List&lt;String&gt;&gt;,因此分配的List必须能够接受任何List&lt;String&gt;作为元素。当你实例化实例时,你试图将其限制为ArrayList&lt;String&gt;

显式的解决方案是:

allData = new ArrayList&lt;List&lt;String&gt;&gt;();

或者更简单地写为:

allData = new ArrayList&lt;&gt;();
英文:

You cannot redefine the generic type of the reference when you instantiate the concrete implementation. The reference is List&lt;List&lt;String&gt;&gt; so the assigned List must be capable of accepting any List&lt;String&gt; as an element. When you instantiated your instance, you attempted to limit this to ArrayList&lt;String&gt;.

The explicit solution is:

allData = new ArrayList&lt;List&lt;String&gt;&gt;();

or more simply as:

allData = new ArrayList&lt;&gt;();

答案3

得分: 1

一个简单的getData示例可能如下所示:

public static List<List<String>> getData(String fileName){
    List<List<String>> content = null;
    try (Stream<String> lines = Files.lines(Paths.get(fileName ))) {
        content = lines
                .map(l -> l.split(" "))
                .map(Arrays::asList)
                .collect(Collectors.toList());

    } catch (IOException e) {
        e.printStackTrace();
    }
    return content;
}
英文:
A simple example of getData might be as below -
        
   public static List&lt;List&lt;String&gt;&gt; getData(String fileName){
        List&lt;List&lt;String&gt;&gt; content = null;
        try (Stream&lt;String&gt; lines = Files.lines(Paths.get(fileName ))) {
            content = lines
                    .map(l -&gt; l.split(&quot; &quot;))
                    .map(Arrays::asList)
                    .collect(Collectors.toList());
    
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }

huangapple
  • 本文由 发表于 2020年9月25日 19:11:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64063000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定