gpt4 book ai didi

java - 从另一个迭代和另一个类添加到 HashSet

转载 作者:行者123 更新时间:2023-12-01 11:20:05 24 4
gpt4 key购买 nike

我正在使用 Apache POI 使用电子表格第 1 工作表中的 3 个值填充 HashSet。由于我还需要访问电子表格的第 2 个表以获取另一个值,因此我再次迭代它:

public class Students {

private int numStudents;
HashSet<Student> studentsRoster1 = new HashSet<Student>();
HashSet<Student> studentsRoster;

public Students(String studentsDb) {

try {
FileInputStream file = new FileInputStream(new File(studentsDb));

XSSFWorkbook workbook = new XSSFWorkbook(file);

DataFormatter fmt = new DataFormatter();

// Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);

String name = null;
String email = null;
int id1 = 0;
String id = null;

Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Student student = new Student();

Row row = rowIterator.next();
// Skip the first row
if (row.getRowNum() == 0) {
continue;
}

Iterator<Cell> cellIterator = row.cellIterator();

while (cellIterator.hasNext()) {

Cell cell = cellIterator.next();

Cell cell0 = row.getCell(0);
Cell cell1 = row.getCell(1);
String formatValue = fmt.formatCellValue(cell1);
Cell cell2 = row.getCell(2);

name = cell0.getStringCellValue();
id1 = (int) cell1.getNumericCellValue();
email = cell2.getStringCellValue();

id = String.valueOf(id1);

student.setName(name);
student.setid(id);
student.setEmail(email);

studentsRoster1.add(student);
}
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

/**** access second sheet for team info ****/
try {
FileInputStream file = new FileInputStream(new File(studentsDb));

XSSFWorkbook workbook = new XSSFWorkbook(file);

// Get second sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(1);

String team = null;

Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Student student = new Student();

Row row = rowIterator.next();
// Skip the first row
if (row.getRowNum() == 0) {
continue;
}

Iterator<Cell> cellIterator = row.cellIterator();

while (cellIterator.hasNext()) {

Cell cell = cellIterator.next();

Cell cell0 = row.getCell(0);

team = cell0.getStringCellValue();

student.setTeam(team);
}
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

this.studentsRoster = studentsRoster1;
}
public HashSet<Student> getStudents() {
return studentsRoster;
}
}

如您所见,我正在创建 HashSet StudentsRoster1;但最终,我需要归还学生名册。我还没有找到将第四个值 student.setTeam(team); 正确添加到 HashSet 的方法。我想创建另一个 HashSet 并使用 union 吗?

我还需要从另一个类添加到 StudentsRoster HashSet,该类正在迭代另一个电子表格以添加另一个值 student.setXXX(xxx);

我也做不到。

非常感谢任何建议。

最佳答案

您可以在一个循环中完成此操作,如下所示:

private static final int NAME_INDEX = 0;
private static final int ID_INDEX = 1;
private static final int EMAIL_INDEX = 2;
private static final int TEAM_INDEX = 0;

private int numStudents;
HashSet<Student> studentsRoster = new HashSet<Student>();


public Students(String studentsDb) {
try {
HashSet<Student> newStudentsRoster = new HashSet<Student>();
FileInputStream file = new FileInputStream(new File(studentsDb));

XSSFWorkbook workbook = new XSSFWorkbook(file);

// Get first sheet from the workbook
XSSFSheet sheet0 = workbook.getSheetAt(0);

// Get second sheet from the workbook
XSSFSheet sheet1 = workbook.getSheetAt(1);

Iterator<Row> rowIterator0 = sheet0.iterator();
Iterator<Row> rowIterator1 = sheet1.iterator();
while (rowIterator0.hasNext() && rowIterator1.hasNext()) {
Row row0 = rowIterator0.next();
Row row1 = rowIterator1.next();
// Skip the first row
if (row0.getRowNum() > 0) {
Student student = new Student();
Iterator<Cell> cellIterator0 = row0.cellIterator();
Iterator<Cell> cellIterator1 = row1.cellIterator();
if (cellIterator0.hasNext()) {
student.setName(row0.getCell(NAME_INDEX).getStringCellValue());
Integer id = row0.getCell(ID_INDEX).getNumericCellValue();
if (id != null){
student.setId(id.toString());
}
student.setEmail(row0.getCell(EMAIL_INDEX).getStringCellValue());
}
if (cellIterator1.hasNext()) {
student.setTeam(row1.getCell(TEAM_INDEX).getStringCellValue());
}
newStudentsRoster.add(student);
}
}
numStudents = newStudentsRoster.size();
studentsRoster = newStudentsRoster;
} catch (FileNotFoundException e) {
e.printStackTrace(); // <- this hides the errors, you must avoid it
} catch (IOException e) {
e.printStackTrace(); // <- this hides the errors, you must avoid it
}
}

关于java - 从另一个迭代和另一个类添加到 HashSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31349982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com