我的 2D ArrayList 语法在 Netbeans 中不起作用

问题描述 投票:0回答:1

我正在 YouTube 上关注 BroCode 的 Java 完整课程并遇到此错误。 NetBeans IDE 不允许使用 BroCode 视频中的语法

ArrayList<ArrayList<String>> groceryList = new ArrayList<String>();
,但它运行良好。

他使用 Eclipse IDE,而我使用 NetBeans。 IDE的差异会影响语法吗?

这是我的代码的屏幕截图:

Here is the screenshot of my code

java arrays multidimensional-array
1个回答
0
投票

变量的类型必须与您创建的对象的类型相匹配;或者成为它的超类型。

您还可以使用菱形运算符,这意味着您可以忽略

<>
之后的
new
字符内的所有内容。

从您的描述中不清楚您想要做什么(我不想点击您问题中的链接),但以下内容应该都有效。 最有可能的是,您想要的是第五个。

// If you really want a list of lists of strings
ArrayList<ArrayList<String>> groceryList = new ArrayList<ArrayList<String>>();  

// The same thing using the diamond operator to save typing
ArrayList<ArrayList<String>> groceryList = new ArrayList<>();  


// If all you want is a list of strings, you could use this
ArrayList<String> groceryList = new ArrayList<String>();  

// Or you could use the diamond operator for this too.
ArrayList<String> groceryList = new ArrayList<>();  

// But once the list is created, nothing in your code should really care 
// whether it's an ArrayList, or some other kind of List, so it makes a
// lot of sense to store it in a List variable, like this.
List<String> groceryList = new ArrayList<>();  
© www.soinside.com 2019 - 2024. All rights reserved.