在Java中将多种数据类型存储到一个数组/表的最佳方法

时间:2019-04-19 02:16:29

标签: java

我有一个正在处理的小项目,我试图在其中从大型.txt文件中提取数据。这些文件大约包含以下100行:

Employee ID -- Salary -- Currently Employed == Employee Name == Paycheck Amounts
1 100 true == Michael == 300 200 100 300
2 200 true == Stephanie == 4000 2300 1000

基本上,我需要稍后再致电Employee ID,并显示他们的薪水,工作情况等。另一个问题是薪水可以是1张薪水或50张

我很好奇您对如何存储此数据有何想法?我可以分界线,什么不是真正得到的,但是一次存储所有东西的最佳方法是什么。

理想情况下,我想打电话给ID 2并查看其Stephanie,她最近的3张薪水分别为4000、2300和1000。

对于我的Java小技巧来说,这似乎是一个艰巨的任务。任何想法/协助将不胜感激!!!

3 个答案:

答案 0 :(得分:2)

这是很标准的东西:

class EmployeeRecord {
  final int employeeId;
  final int salary;
  final boolean isCurrentlyEmployed;
  final String employeeName;
  final List<Integer> paycheckAmounts = new ArrayList<>();

  EmployeeRecord(
      int employeeId,
      int salary,
      boolean isCurrentlyEmployed,
      String employeeName) {
    this.employeeId = employeeId;
    this.salary = salary;
    this.isCurrentlyEmployed = isCurrentlyEmployed;
    this.employeeName = employeeName;
  }
}

将它们放入数组

List<EmployeeRecord> records = new ArrayList<>();

答案 1 :(得分:0)

也许您可以使用地图来存储数据。

Map<String, YourEmployeeObject> records = new HashMap<>();

答案 2 :(得分:0)

创建一个员工类..

class Employee{
final int employeeId;
final int salary;
final boolean isCurrentlyEmployed;
final String employeeName;
final List<Integer> paycheckAmounts = new ArrayList<>();

Employee(
  int employeeId,
  int salary,
  boolean isCurrentlyEmployed,
  String employeeName) {
this.employeeId = employeeId;
this.salary = salary;
this.isCurrentlyEmployed = isCurrentlyEmployed;
this.employeeName = employeeName;
  }
}

然后创建员工对象 并将所有员工添加到员工列表中

List<Employee> employees= new ArrayList<>();