我正在读取Excel工作表并将其存储在数组列表中。它正在存储。但是当我打印数组列表对象时,它会打印相同的数据。请帮助我。
Java代码:-
public class ExcelHelper
{
Employee employee = new Employee();
ArrayList<Employee> employeeList = new ArrayList<Employee>();
public void initialize(String workbookName)
{
try
{
FileInputStream fis = new FileInputStream(workbookName);
Workbook wb = WorkbookFactory.create(fis);
Sheet sh = wb.getSheetAt(0);
int noOfRows = sh.getLastRowNum();
for(int j=1; j<=noOfRows; j++)
{
String operation = sh.getRow(j).getCell(0).getStringCellValue();
if(operation.equalsIgnoreCase("ADD"))
{
String firstName = sh.getRow(j).getCell(2).getStringCellValue();
String lastName = sh.getRow(j).getCell(3).getStringCellValue();
int age = (int)sh.getRow(j).getCell(4).getNumericCellValue();
String gender = sh.getRow(j).getCell(5).getStringCellValue();
int salary = (int)sh.getRow(j).getCell(6).getNumericCellValue();
String department = sh.getRow(j).getCell(7).getStringCellValue();
String state = sh.getRow(j).getCell(8).getStringCellValue();
String city = sh.getRow(j).getCell(9).getStringCellValue();
String address = sh.getRow(j).getCell(10).getStringCellValue();
String email = sh.getRow(j).getCell(11).getStringCellValue();
String skills = sh.getRow(j).getCell(12).getStringCellValue();
employee.setOperation(operation);
employee.setFirstName(firstName);
employee.setLastName(lastName);
employee.setAge(age);
employee.setGender(gender);
employee.setSalary(salary);
employee.setDepartment(department);
employee.setState(state);
employee.setCity(city);
employee.setAddress(address);
employee.setEmail(email);
employee.setSkillSet(skills);
System.out.println(firstName); //Nadn Rohit
employeeList.add(employee);
System.out.println(employeeList.get(j-1).getFirstName()+" from EX"); //Nadn from EX Rohit from EX
}
else if(operation.equalsIgnoreCase("MOD"))
{
int id = (int)sh.getRow(j).getCell(1).getNumericCellValue();
String firstName = sh.getRow(j).getCell(2).getStringCellValue();
String lastName = sh.getRow(j).getCell(3).getStringCellValue();
int age = (int)sh.getRow(j).getCell(4).getNumericCellValue();
String gender = sh.getRow(j).getCell(5).getStringCellValue();
int salary = (int)sh.getRow(j).getCell(6).getNumericCellValue();
String department = sh.getRow(j).getCell(7).getStringCellValue();
String state = sh.getRow(j).getCell(8).getStringCellValue();
String city = sh.getRow(j).getCell(9).getStringCellValue();
String address = sh.getRow(j).getCell(10).getStringCellValue();
String email = sh.getRow(j).getCell(11).getStringCellValue();
String skills = sh.getRow(j).getCell(12).getStringCellValue();
employee.setOperation(operation);
employee.setEmployeeId(id);
employee.setFirstName(firstName);
employee.setLastName(lastName);
employee.setAge(age);
employee.setGender(gender);
employee.setSalary(salary);
employee.setDepartment(department);
employee.setState(state);
employee.setCity(city);
employee.setAddress(address);
employee.setEmail(email);
employee.setSkillSet(skills);
System.out.println(firstName+" ftyg"); //fine
employeeList.add(employee);
}
else
{
int id = (int)sh.getRow(j).getCell(1).getNumericCellValue();
employee.setOperation(operation);
employee.setEmployeeId(id);
employeeList.add(employee);
}
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (EncryptedDocumentException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
for(int i=0; i<employeeList.size(); i++)
{
System.out.println(employeeList.get(i).getFirstName());
//Rohit Rohit
}
}
}
在存储时打印实际数据。存储后将显示相同的数据。例如:-我的输出类似于Rohit Rohit 我的excel看起来像:-
Operation ID FirstName LastName Age Gender Salary Department State City Address Email Skills
ADD Nandn Kumar 19 Male 32000 IT UttarPradesh Lucknow Birsa Road nand00@gmail.com JAVA, SQL
ADD Rohit Kumar 21 Male 25000 IT UttarPradesh Agra Hanuman Gali ro67@gmail.com JAVA, SQL, J2EE
答案 0 :(得分:3)
这里:
employeeList.add(employee);
您一直都在添加非常相同的Employee对象。
相反:创建一个完全为 new 的Employee对象,并继续将这些不同对象添加到您的列表中!
您的工作:
答案 1 :(得分:1)
您已经在课程的第一行中创建了一个名为employee
的对象。在循环内部,将数据分配给从电子表格中读取的该对象,然后将该对象添加到列表中。
第二次循环,使用从电子表格中读取的新数据更改相同对象,然后将其添加到列表中。因此,现在列表包含两个对象,它们都有相同的 reference (实际上,列表中只有两个标签,但是它们是同一对象)。这意味着当您第二次更改对象时,它会更新该对象中所有已存在对象的信息,即已存在于列表中的对象!
此问题很容易解决;您不必在课程开始时创建一个Employee
对象,而可以在顶部的Employee
循环内创建一个for
对象。这样,每次循环时,您都将创建一个全新的Employee
对象,该对象将在迭代过程中捕获所需的不同数据。希望下面的代码片段有助于说明我的意思:
public class ExcelHelper
{
// Employee employee = new Employee(); // **REMOVE THIS LINE**
... // details omitted
for(int j=1; j<=noOfRows; j++)
{
Employee employee = new Employee();
... // details omitted
}
希望这会有所帮助!