简体   繁体   English

将对象存储在数组中,我的值在数组中不断变化

[英]Storing objects in an array, my values keep changing within my array

So I have a while loop which stores all of my variables, but my problem is that whenever the program loops it changes the values of the object in the previous array.所以我有一个 while 循环来存储我的所有变量,但我的问题是,每当程序循环时,它都会更改前一个数组中对象的值。 So employeeArray is filled with all objects of the same values rather than storing the previous value and creating a new one.因此,employeeArray 填充了具有相同值的所有对象,而不是存储以前的值并创建一个新的值。 I am reading text from a .csv file.我正在从 .csv 文件中读取文本。 Can somebody please explain to me how to store my Employee objects without them changing each loop?有人可以向我解释如何在不改变每个循环的情况下存储我的 Employee 对象吗? Let me know if you need any clarification I know I probably missed some information somebody trying to help me may need.如果您需要任何说明,请告诉我,我知道我可能错过了一些试图帮助我的人可能需要的信息。 Anyways my code is below, I have 3 different classes but Im just going to put the Employee and EmpQuery class on here.无论如何,我的代码在下面,我有 3 个不同的类,但我只想将 Employee 和 EmpQuery 类放在此处。 I believe the problem is with my variables in the Employee class.我相信问题出在 Employee 类中的变量上。 PLEASE HELP ME, it would be greatly appreciated.请帮助我,将不胜感激。

public class EmpQuery extends Employee {

    public static void fillArrayObjects(Scanner s, Employee[] e){

        //VARIABLES
        String sNextLine;
        int counter = 0;
        int parsedString;

        String employeeID;
        String employeeName;
        String employeeDepartment;
        String employeeStartDate;
        int employeeEarnings;

        //DECLARE ARRAY TO HOLD EMPLOYEE OBJECTS
        Employee employeeArray[] = new Employee [50];//50 records I believe

        while(s.hasNext()){

            //SCANNER AND SEPARATE STRING VALUE IN LINE
            sNextLine = s.nextLine(); 

            String[] tempSplit = sNextLine.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1);

            //REMOVE EVERYTHING EXPCEPT NUMBERS [4]
            tempSplit[4] = tempSplit[4].replace(",","");
            tempSplit[4] = tempSplit[4].replace("$","");
            tempSplit[4] = tempSplit[4].replace("\"", "");
            tempSplit[4] = tempSplit[4].replace(".00","");
            parsedString = Integer.parseInt(tempSplit[4]);

            //STORE TEMP SPLITS IN NEW VARIABLES
            employeeID = tempSplit[0];
            employeeName = tempSplit[1];
            employeeDepartment = tempSplit[2];
            employeeStartDate = tempSplit[3];
            employeeEarnings = parsedString;

            //CALLING FROM EMPLOYEE CLASS
            Employee.enterData(employeeID, employeeName, employeeDepartment, employeeStartDate, employeeEarnings);

            //STORE EACH NEW EMPLOYEE IN employeeArray
            Employee i = new Employee(employeeID, employeeName, employeeDepartment, employeeStartDate, employeeEarnings);
            employeeArray[counter] = i;

            //TESTS
            System.out.println(counter + "  " +  Employee.getEarnings(employeeArray[0]));//employee[1] keep changing every loop            

            //INCREMENT COUNTER
            counter++;

        }

        System.out.println(Employee.getEarnings(employeeArray[12]));
        System.out.println(Employee.getID(employeeArray[3]));

    }

*************************************NEW CLASS******************************

package employeedb;包员工数据库;

/** * * @author Daniel */ public class Employee { /** * * @author Daniel */ public class Employee {

//VARIABLES***************************************************************************************************
public String empID;
public String empName;
public String department;
public String startDate;
public int earnings;

Employee newGuys;



//EMPLOYEE*********************************************************************************************************
public Employee(){

    empID = "";
    empName = "";
    department = "";
    startDate = "";
    earnings = 0;
}


public Employee(String iD, String name, String employeeDepartment, String startingDate, int salary){

    empID = iD;
    empName = name;
    department = employeeDepartment;
    startDate = startingDate;
    earnings = salary;        

}

public Employee(String iD, String name){

    empID = iD;
    empName = name;

}



//ENTER DATA*******************************************************************************************************
public void enterData(){

    empID = "";
    empName = "";
    department = "";
    startDate = "";
    earnings = 0;

}


//ENTER DATA
public void enterData(String iD, String name){

    empID = iD;
    empName = name;
    department = "";
    startDate = "";
    earnings = 0;

}


//ENTER DATA
public void enterData(String iD, String name, String employeeDepartment, String startingDate, int salary){

    empID = iD;
    empName = name;
    department = employeeDepartment;
    startDate = startingDate;
    earnings = salary;

}



//VIEW SPECIFIC FIELD****************************************************************************************************
public void viewEmployeeID(Employee variable){

    System.out.println(empID);

}


public void viewEmployeeName(Employee variable){

    System.out.println(empName);

}


public void viewDepartment(Employee variable){

    System.out.println(department);

}


public void viewStartDate(Employee variable){

    System.out.println(startDate);

}


public void viewEarnings(Employee variable){

    System.out.println(earnings);

}



//VIEW DATA**********************************************************************************************************
public void viewAllData(){

    empID = "";
    empName = "";
    department = "";
    startDate = "";
    earnings = 0;

    System.out.println("Employee ID:    " + empID);
    System.out.println("Employee name:    " + empName);
    System.out.println("Employee department:    " + department);
    System.out.println("Employee start date:    " + startDate);
    System.out.println("Employee earnings:    $" + earnings);
    System.out.println("");

}


//VIEW DATA
public void viewData(String iD, String name, String employeeDepartment, String startingDate, int salary){

    empID = iD;
    empName = name;
    department = employeeDepartment;
    startDate = startingDate;
    earnings = salary;

    System.out.println("Employee ID:    " + empID);
    System.out.println("Employee name:  " + empName);
    System.out.println("Employee department:    " + department);
    System.out.println("Employee start date:    " + startDate);
    System.out.println("Employee earnings:  $" + earnings);
    System.out.println("");

}



//RETURN DATA*********************************************************************************************************

//GET ID
public String getID(Employee variable){//void

    return empID;

}


//GET NAME
public String getName(Employee variable){

    return empName;

}


//GET DEPARTMENT
public String getDepartment(Employee variable){

    return department;

}


//GET START DATE
public String getStartDate(Employee Variable){

    return startDate;

}


//GET EARNINGS
public int getEarnings(Employee Variable){

    return earnings;

}

} }

The main problem is that you set the variables in Employee as static, that means they are shared in every instance of the class.主要问题是您将Employee的变量设置为静态变量,这意味着它们在类的每个实例中都是共享的。 They should not be static so every object has their own.它们不应该是静态的,所以每个对象都有自己的。

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

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