简体   繁体   English

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

[英]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. 我有一个正在处理的小项目,试图在该项目中从庞大的.txt文件中提取数据。 The files got about 100 rows of the following: 这些文件包含以下内容的约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

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 从本质上讲,我需要能够在以后致电Employee ID,并显示他们的薪水,工作情况等。另一个问题是薪水可以是1薪水或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. 理想情况下,我想打电话给ID 2并查看其Stephanie,她最近的3张工资分别是4000、2300和1000。

This seems like a bit of a big task for my small Java skills. 对于我的小型Java技能来说,这似乎是一项艰巨的任务。 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<>();

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

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