[英]Adding object to an array list in Java
I am trying to add an object to an array list and have the following -我正在尝试将 object 添加到数组列表中并具有以下内容 -
public static void main(String[] args) {
authors Authors = new authors();
ArrayList<authors> tabAuthors = new ArrayList<authors>();
Authors.setAuthId(1);
Authors.setAuthName("Roald Dahl");
System.out.println(Authors.toString());
tabAuthors.add(Authors);
Authors.setAuthId(2);
Authors.setAuthName("Julia Donaldson");
System.out.println(Authors.toString());
tabAuthors.add(Authors);
for (int counter =0; counter < tabAuthors.size(); counter++) {
System.out.println(tabAuthors.get(counter).getAuthId() + " " + tabAuthors.get(counter).getAuthName() );
}
}
}
The authors class作者 class
public class authors {
private int authId;
private String authName;
public int getAuthId() {
return authId;
}
public void setAuthId(int authId) {
this.authId = authId;
}
public String getAuthName() {
return authName;
}
public void setAuthName(String authName) {
this.authName = authName;
}
@Override
public String toString() {
return "authors{" +
"authId=" + authId +
", authName='" + authName + '\'' +
'}';
}
}
I was expecting the code to return - 1 Roald Dahl 2 Julia Donaldson我期待代码返回 - 1 Roald Dahl 2 Julia Donaldson
Instead, I am getting - 2 Julia Donaldson 2 Julia Donaldson相反,我得到 - 2 Julia 唐纳森 2 Julia 唐纳森
Why is the array list not reflecting the first object values?为什么数组列表不反映第一个 object 值?
Because you create only one object and override second time when you set properties.因为您只创建了一个 object 并在设置属性时第二次覆盖。
You need to create new object when you insert second object.插入第二个 object 时,需要创建新的 object。
You are setting the value Julia Donaldson
on the same Object.您正在同一 Object 上设置值
Julia Donaldson
。 reference the variable Authors
to a new object and the problem is solved.将变量
Authors
引用到新的 object 并解决问题。
authors Authors = new authors();
ArrayList<authors> tabAuthors = new ArrayList<authors>();
Authors.setAuthId(1);
Authors.setAuthName("Roald Dahl");
System.out.println(Authors.toString());
tabAuthors.add(Authors);
Authors = new Authors(); //Reference Authors to a new object here, or else
//you're using the same object and you'll overwrite the value Roald Dahl with
//Julia Donaldson
Authors.setAuthId(2);
Authors.setAuthName("Julia Donaldson");
System.out.println(Authors.toString());
tabAuthors.add(Authors);
In addition to the answer above by @Maurice, you can also add a constructor
in authors
class to set values for every object.除了@Maurice 上面的答案之外,您还可以在
authors
class 中添加一个constructor
来为每个 object 设置值。
public authors(int authId, String authName) {
super();
this.authId = authId;
this.authName = authName;
}
In your main()
, create two objects as below and add them to the list:在您的
main()
中,创建如下两个对象并将它们添加到列表中:
public static void main(String[] args) {
Authors authors1 = new Authors(1, "Roald Dahl");
Authors authors2 = new Authors(2, "Julia Donaldson");
List<Authors> tabAuthors = new ArrayList<Authors>();
System.out.println(authors1.toString());
tabAuthors.add(authors1);
System.out.println(authors2.toString());
tabAuthors.add(authors2);
for (int counter = 0; counter < tabAuthors.size(); counter++) {
System.out.println(tabAuthors.get(counter).getAuthId() + " " + tabAuthors.get(counter).getAuthName());
}
}
Output: Output:
authors{authId=1, authName='Roald Dahl'}
authors{authId=2, authName='Julia Donaldson'}
1 Roald Dahl
2 Julia Donaldson
Thanks for your prompt responses.感谢您的及时回复。
I added the below in the code as I need to add indeterminate number of objects - tabAuthors.add(new authors(authId,authName,authCountry,numBooks));我在代码中添加了以下内容,因为我需要添加不确定数量的对象 - tabAuthors.add(new authors(authId,authName,authCountry,numBooks));
and declared authId,authName,authCountry,numBooks as variables.并将 authId,authName,authCountry,numBooks 声明为变量。 The object values are being fetched from a database table.
正在从数据库表中获取 object 值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.