Java 避免覆盖 ArrayList 中的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20168524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:40:19  来源:igfitidea点击:

Avoiding overwriting objects in ArrayList

javaarraylist

提问by Jesper Bruun Hansen

Stackers. I've been searching the site for my question, but didn't find what I was looking for. I'm stuck with this code:

堆垛机。我一直在网站上搜索我的问题,但没有找到我要找的东西。我被这个代码困住了:

public class Users{
ArrayList<ValidateUser> personer = new ArrayList<ValidateUser>();
ValidateUser newUser = new ValidateUser();
    newUser.setUser("administrator");
    newUser.setPass("asdf123");
    newUser.setBalance(0.8);
    newUser.setType("admin");
    personer.add(newUser);

Got a nice array-list going on, but if i add more "newUsers" to the ArrayList, they seem to overwrite each other. I don't want to make a newUser1, newUser2 object, since later in my program, I have to be able to add new users, directly from the program.

有一个不错的数组列表,但是如果我向 ArrayList 添加更多“newUsers”,它们似乎会相互覆盖。我不想创建 newUser1、newUser2 对象,因为在我的程序稍后,我必须能够直接从程序添加新用户。

How to achieve this?

如何实现这一目标?

ValidateUser:

验证用户:

public class ValidateUser {

private String username;
private String password;
private double balance;
private String role;


public void setUser(String user) {
    username = user;
}
public void setPass(String pass) {
    password = pass;
}
public void setBalance(double rating) {
    balance = rating;
}
public void setType(String type) {
    role = type;
}

public String getUsername() {
    return username;
}
public String getPassword() {
    return password;
}
public double getBalance() {
    return balance;
}
public String getRole() {
    return role;
}

}

}

采纳答案by Kuba Spatny

If I understood right you're adding new users this way:

如果我理解正确,您将通过这种方式添加新用户:

ValidateUser newUser = new ValidateUser();
    newUser.setUser("administrator");
    newUser.setPass("asdf123");
    newUser.setBalance(0.8);
    newUser.setType("admin");
    personer.add(newUser);

    newUser.setUser("different admin");
    personer.add(newUser);

however this way the object points to the same reference, thus you must do the following to instantiate a new object:

但是这样对象指向相同的引用,因此您必须执行以下操作来实例化一个新对象:

 newUser = new ValidateUser();
 newUser.setUser("foo");
 personer.add(newUser);

回答by DejanLekic

As long as you new ValidateUser()you should be fine. In other words, if you instantiate a new object of the ValidateUser type, and add it to your ArrayList.

只要new ValidateUser()你应该没问题。换句话说,如果您实例化一个 ValidateUser 类型的新对象,并将其添加到您的 ArrayList。

You did not clearly explain what you exactly do, but my first guess is that you use the same reference all over again... :)

你没有清楚地解释你到底在做什么,但我的第一个猜测是你再次使用相同的参考...... :)

Example how to make 10 new ValidateUser objects:

示例如何创建 10 个新的 ValidateUser 对象:

// Let's make 10 objects of ValidateUser type
ValidateUser tmpVuser = null;
for (int i = 0; i < 10; i++) {
    tmpVuser = new ValidateUser(); // notice: we always create a new instance
    tmpVuser.setUser("user" + i);
    tmpVuser.setPass("changeme" + i);
    tmpVuser.setBalance(0.8);
    tmpVuser.setType("admin");
    personer.add(tmpVuser);
}

However, it is worth of notice that if ValidateUser was an immutable type (more about it here: http://www.javapractices.com/topic/TopicAction.do?Id=29), your code would probably work fine.

然而,值得注意的是,如果 ValidateUser 是一个不可变类型(更多关于它的信息在这里:http: //www.javapractices.com/topic/TopicAction.do?Id=29),您的代码可能会正常工作。