简体   繁体   English

将类对象存储到数组中

[英]Storing Objects of Class into an Array

I'm trying to store objects that have a username and password from the class "Driver" into an array list. 我正在尝试将具有“ Driver”类的用户名和密码的对象存储到数组列表中。 When I try to print every value in the array to test whether they're being stored, it only prints the last value declared, numerous times. 当我尝试打印数组中的每个值以测试它们是否被存储时,它只会多次打印声明的最后一个值。 I have tried nearly every other solution on thee forums related to this issue and it just wont work :( 我已经在与该问题相关的论坛上尝试了几乎所有其他解决方案,但它不会起作用:(

Code below: 代码如下:

package eDepotSystem;

import java.util.ArrayList;


public class Driver {

    protected static String driverUserName;
    protected static String driverPassWord;

    public Driver (String userName, String passWord) {
        driverUserName = userName;
        driverPassWord = passWord;

    }

    public static void driverArray() {

        ArrayList<Driver> driverList = new ArrayList<Driver>();

        Driver driver = new Driver(driverUserName, driverPassWord);
        driver.setUserName("driver1");
        driver.setPassword("123");
        driverList.add(driver);

        driver = new Driver(driverUserName, driverPassWord);
        driver.setUserName("driver2");
        driver.setPassword("321");
        driverList.add(driver);

        Driver tempDriver = new Driver(driverUserName, driverPassWord);
        for (int i = 0; i < driverList.size(); i++) {
            tempDriver = driverList.get(i);
            System.out.println(tempDriver);
        }   
    }

    public void setPassword(String password) {
        driverPassWord = password;
    }

    public static String getUserName() {
        return driverUserName;
    }

    @Override
    public String toString() {
        return driverUserName + driverPassWord;
    }
}

I don't know whether my loop is wrong or the way I'm declaring the objects is wrong? 我不知道我的循环是错误的还是我声明对象的方式是错误的? Any help would be grand and thanks in advance! 任何帮助将是巨大的,在此先感谢!

your field variables should not be static. 您的字段变量不应为静态。 It is being shared by all instances of Driver class (ie objects), hence it is printing the last value which you added. 它由Driver类的所有实例(即对象)共享,因此它将打印您添加的最后一个值。

Problem 1: 问题一:

Your "instance variables" (username and password) are static. 您的“实例变量”(用户名和密码)是静态的。 Therefore you only have one instance of them. 因此,您只有一个实例。 If you print them out you must always get the same value. 如果将它们打印出来,则必须始终获得相同的值。

Problem 2: 问题2:

You only add one object. 您只添加一个对象。 You add it once, change it's values and add it a second time. 您添加一次,更改其值,然后第二次添加。 If you print it out you must get the same values ... even if you remove the static keywords. 如果将其打印出来,即使您删除了static关键字,也必须获得相同的值。

You should instead try something like this: 您应该尝试这样的事情:

package eDepotSystem;

import java.util.ArrayList;
import java.util.List;

public class Driver {
    private final String driverUserName;
    private final String driverPassWord;

    public Driver (String userName, String passWord) {
        driverUserName = userName;
        driverPassWord = passWord;
    }

    public static void driverArray() {
        List<Driver> driverList = new ArrayList<Driver>();
        driverList.add(new Driver("drv1", "pw1"));
        driverList.add(new Driver("drv2", "pw2"));

        for (Driver tempDriver : driverList) {
            System.out.println(tempDriver);
        }   
    }
}

The static keyword forces the driverUserName and driverPassWord variables to be instantiated only once in memory. static关键字强制将driverUserName和driverPassWord变量在内存中仅实例化一次 While they are not constants, it makes any further additions to your list reference that first and only instance in memory, hence why it keeps showing the same value. 尽管它们不是常量,但它会向列表引用中的第一个(也是唯一一个)内存实例进行进一步的添加,因此为什么它始终显示相同的值。

https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM