Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用for循环向数组添加对象时,添加的最后一个对象将用于所有实例_Java_Arrays_For Loop - Fatal编程技术网

Java 使用for循环向数组添加对象时,添加的最后一个对象将用于所有实例

Java 使用for循环向数组添加对象时,添加的最后一个对象将用于所有实例,java,arrays,for-loop,Java,Arrays,For Loop,我一直在尝试使用for循环将驱动程序的实例添加到驱动程序数组中。每个驱动程序都有3个基本变量,这些变量通过for循环收集。当循环运行时,最后一个驱动程序的详细信息存储在数组的所有索引中!我想得到它,这样我就可以将每个单独的驱动程序添加到数组中 public static void addDriver(Driver[] d) { //method using for loop to add drivers for(int i = 0; i < d.length; i++

我一直在尝试使用for循环将驱动程序的实例添加到驱动程序数组中。每个驱动程序都有3个基本变量,这些变量通过for循环收集。当循环运行时,最后一个驱动程序的详细信息存储在数组的所有索引中!我想得到它,这样我就可以将每个单独的驱动程序添加到数组中

public static void addDriver(Driver[] d) {      //method using for loop to add drivers


    for(int i = 0; i < d.length; i++ ) {


        String name, DOB, occupation;

        System.out.println("Please Enter Driver Name");
        name = kb.nextLine();

        System.out.println("Please Select Driver Occupation");
        System.out.println("1: Chauffeur" + "\n2: Accountant");
        int choice = kb.nextInt();
        kb.nextLine();

        if (choice == 1) {
            occupation = "Chauffeur";
        } else {
            occupation = "Accountant";
        }

        System.out.println("Please Enter Driver D.O.B");
        DOB = kb.nextLine();

        d[i]  = new Driver(name, occupation, DOB);

    }
}

我已经为这个挠头好一阵子了,因为我认为它是正确的。谢谢你迄今为止的帮助

在您的
Driver
类中,您将三个全局变量
名称
职业
D.O.B
定义为
静态
。这意味着,无论何时更改该变量的值,它都将在程序中的任何地方更改,即使您创建了该类的多个实例。只要去掉静态声明,问题就解决了

在循环之前,
d.length
的值是多少?除非我遗漏了什么,否则它应该可以工作。你能把你调用这个方法并打印结果的代码贴在哪里吗?我认为这里没有任何错误。正如@JamalH所说,在调用此方法的地方发布代码,这可能会有所帮助。请添加
驱动程序
类定义。
public static void main(String[] args) {

    int drivers = driverNum();      //Setting size of the array

    Driver[] d = new Driver[drivers];       //creating new array using number of drivers to be insured

    addDriver(d);       //calling method to add drivers to array

    for(int x = 0; x < d.length; x++)
    {
        System.out.println(d[x].toString());
    }

}
public class Driver {

static String name, occupation, DOB;

public Driver()
{
    name = "";
    occupation = "";
    DOB = "";
}

public Driver(String name, String occupation, String DOB)
{
    this.name = name;
    this.occupation = occupation;
    this.DOB = DOB;
}

public void setName(String name)
{
    this.name = name;
}

public String getName(Driver d)
{
    return name;
}

public void setOccupation(String occupation)
{
    this.occupation = occupation;
}

public String getOccupation()
{
    return occupation;
}

public void setDOB(String DOB)
{
    this.DOB = DOB;
}

public String getDOB()
{
    return DOB;
}

public String toString()
{
    String s;

    s = "Name: " + name;
    s = s + "\nOccupation: " + occupation;
    s = s + "\nDOB: " + DOB;

    return s;
}
}