简体   繁体   中英

Best way to store multiple data types to one array / table in java

I've got a small project I'm working on in which I'm trying to be able to pull data from a massive .txt file. The files got about 100 rows of the following:

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

Essentially I need to be able to call Employee ID at a later date and it shows their salary, employment etc. The other issue is that the paychecks could be either 1 paycheck or 50

I'm curious what are your thoughts on how to store this data? I can split the lines and what not to actually get it but what's the best method of storing it all at once.

Ideally what I would like to do is be able to call ID 2 and see its Stephanie and her last 3 paychecks were 4000, 2300 and 1000.

This seems like a bit of a big task for my small Java skills. Any thoughts / assitance would be highly appreciated!!!

This is pretty standard stuff:

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;
  }
}

Put these in an array

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

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

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

Create a Employee Class ..

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;
  }
}

Then create employee object And add all employees to the list of employees

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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