英文:
How do you add to an ArrayList using generics?
问题
我在处理我的作业时遇到了困难,我无法弄清楚如何将另一个元素添加到我的列表中。
import java.util.ArrayList;
public class Ballot {
private ArrayList<Candidate> ballot;
private String officeName;
public Ballot(String officeName) {
this.officeName = officeName;
ArrayList<Candidate> ballot = new ArrayList<Candidate>();
}
public String getOfficeName() {
return officeName;
}
public void addCandidate(Candidate c) {
ballot.add(c);
}
public ArrayList<Candidate> getCandidates() {
return ballot;
}
public static void main(String[] args) {
Ballot b = new Ballot("Election");
b.addCandidate(new Candidate("Sarah", "President"));
System.out.println(b);
}
}
当我尝试运行文档时,它抛出了一个NullPointerException
。我做错了什么?
英文:
I'm struggling with an assignment of mine and I can't figure out how to add another element to my list.
import java.util.ArrayList;
public class Ballot {
private ArrayList<Candidate> ballot;
private String officeName;
public Ballot(String officeName) {
this.officeName = officeName;
ArrayList<Candidate> ballot = new ArrayList<Candidate>();
}
public String getOfficeName() {
return officeName;
}
public void addCandidate(Candidate c) {
ballot.add(c);
}
public ArrayList<Candidate> getCandidates() {
return ballot;
}
public static void main(String[] args) {
Ballot b = new Ballot("Election");
b.addCandidate(new Candidate("Sarah", "President"));
System.out.println(b);
}
}
When I try to run the document, it throws a NullPointerException
. What am I doing wrong?
答案1
得分: 3
构造函数初始化了一个名为 ballot
的局部变量,该变量隐藏了相同名称的数据成员。然后,当您尝试向其添加内容时,由于它从未被初始化,会导致NullPointerException
。如果您进行初始化,应该就没问题了:
public Ballot(String officeName) {
this.officeName = officeName;
ballot = new ArrayList<Candidate>(); // 在这里进行初始化!
}
英文:
The constructor initializes a local variable named ballot
that hides the data member with the same name. Then, when you try to add to it, it fails with a NullPointerException
, since it was never initialized. If you initialize it you should be OK:
public Ballot(String officeName) {
this.officeName = officeName;
ballot = new ArrayList<Candidate>(); // Here!
}
答案2
得分: 1
你没有在 Ballot
构造函数中正确初始化候选人列表。你需要这样做:
this.ballot = new ArrayList<Candidate>();
当前你只是在构造函数中创建了一个名为 ballot
的局部变量,它遮蔽了实际的类字段。由于它从未被初始化,所以当你尝试向其中添加元素时最终会得到一个 NullPointerException
。
另外,作为最佳实践,使用接口而不是具体类型。这样可以方便以后更改实现。所以,不要将字段定义为 private ArrayList<Candidate> ballot;
,而是定义为 private List<Candidate> ballot;
。
英文:
You're not initializing your list of candidates properly in the Ballot
constructor. You need to do:
this.ballot = new ArrayList<Candidate>();
Right now you're just creating a local variable named ballot
in the constructor which shadows the actual class field. Since it has never been initialized, you end up getting a NullPointerException
when you eventually try to add an element to it.
Also, as a best practice, use interfaces instead of the concrete type. This makes it easy to change implementations later. So instead of defining the field as private ArrayList<Candidate> ballot;
, define it as private List<Candidate> ballot;
.
答案3
得分: 1
只返回翻译好的部分:
就像你没有使用 this
对象一样简单。你从未初始化你的对象。
正确的方式:
public Ballot(String officeName) {
this.officeName = officeName;
this.ballot = new ArrayList<Candidate>();
}
英文:
As simple that you are not using this
object. You are never initiliazing your object
Correct way
public Ballot(String officeName) {
this.officeName = officeName;
this.ballot = new ArrayList<Candidate>();
}
答案4
得分: 1
你正在用同名的局部变量覆盖了类变量。要么直接初始化列表:
private List<Candidate> ballot = new ArrayList<>();
要么在构造函数中进行初始化:
ballot = new ArrayList<>();
顺便提一下:如果可能的话,不应该为局部变量和返回值分配实现类。 "ballot" 应该只是 List 接口,getter 也应如此。这样,如果将来想要更改实现,就不必修改所有内容。它可以是 ArrayList、LinkedList、Stack、Vector 等,而不影响,因为它们都使用 List 接口。
英文:
You're overriding your class variable with a local variable of the same name. Either initialize the list directly
private List<Candidate> ballot = new Arraylist<>();
or initialize it in the constructor with
ballot = new ArrayList<>();
FYI: You shouldn't assign implementation classes for your local variables and return values if you can help it. "ballot" should just be the List interface as should the getter. That way if you ever want to change the implementation, you don't have to change everything. It could be an ArrayList, LinkedList, Stack, Vector, etc and it won't matter because they're all using the List interface.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论