Java:ListA.addAll(ListB) 引发 NullPointerException?
err 部分在代码中大写,它也出现在 foreaching 中。由于抽象列表,它无法初始化,声明位于静态字段中。这些列表具有相同的类型。
import java.util.*;
public class Test
{
public static final List<String> highPrio = Arrays.asList("*","/");
public static List<String> ops;
public static void main(String[] args)
{
//ERROR HERE, why do it throw nullPointer?
ops.addAll(highPrio);
for(String s : ops)
{
System.out.println(s);
}
}
}
为什么初始化时不使用new List()?
不初始化的原因是无法使用= new List
。我看不出有什么逻辑不允许这样做。它一定与数据结构等内在因素有关。
Test.java:7: java.util.List 是抽象的;无法实例化 公共静态列表
为什么list是一个接口?
我知道很多数据结构比如stack都实现了list。但我无法理解为什么 List 是一个接口,而为什么 Table 不是一个接口。我将列表视为一种原始结构,您可以使用它来实现其他结构。接口是您可以指定结构要求的东西。成为接口的原因是原始性还是广泛性?
The err part is Capitalized in the code, it also comes in foreaching. Because of the abstract list, it cannot be initialized, declaration is in a static field. The lists have the same type.
import java.util.*;
public class Test
{
public static final List<String> highPrio = Arrays.asList("*","/");
public static List<String> ops;
public static void main(String[] args)
{
//ERROR HERE, why do it throw nullPointer?
ops.addAll(highPrio);
for(String s : ops)
{
System.out.println(s);
}
}
}
Why not new List() in the initialization?
The reason for not initialization was the inability to use = new List<String>()
. I cannot see a logic not allowing it. It must have something to do with intrinsic factors such as data strucs or something else.
Test.java:7: java.util.List is abstract; cannot be instantiated
public static List<String> ops = new List<String>();
Why list is an interface?
I know that many data strucs such as stack implements list. But I cannot understand why List is an interface and why not Table for example. I see list as a primitive structure with which you can implement other structures. Interface is a thing where you can specify requirements for a structure. Is the primitivenness or extensivenss reason for being an interface?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
因为 ops 为空。 List 是一个接口这一事实并不意味着您无法初始化该字段:
List
是一个接口,因为在提供相同约定的同时有多种实现它的方法(尽管性能特征不同)。例如,ArrayList 是数组支持的,而 LinkedList 是链接列表。Because ops is null. The fact that List is an interface does not mean you can't initialize the field:
List
is an interface because there are multiple ways of implementing it while providing the same contract (though different performance characteristics). For instance,ArrayList
is array-backed, whileLinkedList
is a linked list.您需要实例化操作列表。
或您选择的其他列表类型。
You need to instantiate the ops list.
or another list type of your choosing.
您还没有初始化列表操作;
例如,
或者你可以这样做
You have not initialized List ops;
e.g.
Alternatively you could do
ops 尚未初始化。
将声明更改为:
ops hasn't been initialized yet.
change declaration to:
ops 从未初始化。
在执行 addAll 命令之前,您必须执行
ops = new ArrayList();
。否则你将调用一个空对象。不能执行
ops = new List
' 的原因是 List 是一个接口,无法初始化。 ArrayList 不是抽象类型并且扩展了 List,因此在这种情况下它是合适的。抽象类型和接口不能创建为实际对象,只能用于引用某些具体对象。具体类型必须扩展您尝试使用的抽象类型或接口。
ops is never initialized.
You have to do
ops = new ArrayList<String>();
before you do the addAll command. Otherwise you are calling a null object.The reason that you can't do
ops = new List<String>
' is because List is an interface and cannot be initialized. ArrayList is not an abstract type and extends List, so it is appropriate in this context.Abstract types and interfaces cannot be created as an actual object, and can only be used to reference some concrete object. The concrete type must extend the abstract type or interface which you are trying to use.
你要添加什么?变量
ops
仍然为 null,就像下面的s
为 null 一样:You are adding to what? Variable
ops
is still null, just likes
is null in the following :我认为您真正的问题是:
为什么我不能实例化列表?
或 为什么我得到:
Test.java:7: java.util.List is Abstract;无法实例化 public static List; ops = new List();
在 Java 中,List 是一个接口,正如你所说:
接口是一个可以指定结构要求的东西 - 它就像需要填写的工作描述。但是你不能把工作描述从纸上剪下来,然后简单地用它来完成工作,即
List
工作描述是抽象的;无法实例化
。相反,您需要候选人来填补
List
职位描述中描述的职位。满足作业要求的候选(“具体实现”)有ArrayList
、LinkedList
、Vector
等。除非您初始化 <代码>列表<字符串>如果您将 ops var 指定为特定候选人来完成这项工作,则没有人 (null
) 实际完成这项工作(从而引发NullPointerException
)。I think your real question is:
Why can't I instantiate a List?
or Why am I getting:
Test.java:7: java.util.List is abstract; cannot be instantiated public static List<String> ops = new List<String>();
In Java, List is an interface, which is, as you say:
Interface is a thing where you can specify requirements for a structure
- its like a job description that needs to be filled. But you can't clip the job description out of the paper and simply use it to do the job, i.e. theList
job descriptionis abstract; cannot be instantiated
.Instead, you need candidates to fill the position described by the job description for
List
. The candidates ("concrete implementations") that meet the requirements for the job areArrayList
,LinkedList
,Vector
, etc. Unless you initialize yourList<String> ops
var with a specific candidate to do the job, you've got no one (null
) to actually do the work (there by raising aNullPointerException
.