简体   繁体   English

如何在Java中存储worker对象列表?

[英]How to store list of workers objects in java?

What is the most convenient way to store list of workers in Java considering following conditions: 考虑到以下条件,用Java存储工作人员列表的最便捷方法是:

  1. Each worker has attributes of Name, LastName, Wage, BirthDate. 每个工作人员都具有名称,姓氏,工资,出生日期的属性。 2.I want to store them in file by appending when needed. 2.我想在需要时通过附加将它们存储在文件中。 For example 1 object I added to file today, 1 tomorrow, 3 in one month and so on. 例如,我今天添加了1个对象,明天添加了1个对象,一个月后添加了3个对象,依此类推。
  2. I want to read them also when needed, retrieve, sort, delete, edit and perform other operations. 我也想在需要时阅读它们,检索,排序,删除,编辑和执行其他操作。
  3. I tried to store them in file with ObjectOutputStream, but the problem is I need to store them at once, so appending later is not possible. 我尝试使用ObjectOutputStream将它们存储在文件中,但是问题是我需要立即存储它们,因此以后无法追加。

So my question is which type of storing to implement? 所以我的问题是要实现哪种存储类型?

I suppose workers is a struct... Have you thought on using an array of workers? 我想工人是一个结构...您是否考虑过使用一系列工人? or a list? 或清单?

So here's the thing I think you're missing: ObjectOutputStream is a method for carrying out serialization (the converting of an object to a writable value). 所以这是我认为您缺少的东西:ObjectOutputStream是一种执行序列化(将对象转换为可写值)的方法。 So, to use OOS, you have to implement that in your worker class to allow it to be serialized on its own. 因此,要使用OOS,您必须在worker类中实现它,以使其能够独立进行序列化。 But, since you want to store more than one of these, you can use one of the many java storage mechanisms (all of which implement serializable). 但是,由于您想存储其中的一种以上,因此可以使用许多Java存储机制之一(所有这些机制都实现可序列化)。

For this particular situation, I'd recommend using an ArrayList. 对于这种特殊情况,我建议使用ArrayList。 Put all of the worker objects in an ArrayList and then send that list to the ObjectOutputStream. 将所有辅助对象放入ArrayList中,然后将该列表发送到ObjectOutputStream。 Then, when you pull the list back into your code from the file, you can continue to add more workers to it as you please and then you can serialize the updated list without a problem. 然后,当您将列表从文件中拉回到代码中时,可以继续根据需要向其添加更多工作程序,然后可以序列化更新后的列表而不会出现问题。

Once you have the list pulled into your code, you can use myList.get(index) , myList.remove(index) and all of the other methods that you can find here: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html 将列表放入代码后,就可以使用myList.get(index)myList.remove(index)以及可以在此处找到的所有其他方法: https : myList.remove(index) 8 / docs / api / java / util / ArrayList.html

As an aside, I think an ArrayList would be better than a normal array because when you declare and instantiate a normal array you have to specify the length of the array. 顺便说一句,我认为ArrayList会比普通数组更好,因为在声明和实例化普通数组时,必须指定数组的长度。 When you use the ArrayList, you can have as many objects as you want in the list. 使用ArrayList时,列表中可以有任意多个对象。

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

相关问题 如何在java中存储和读取对象的数组列表? - How to store and read an array list of objects in java? Java如何在List中存储对象? - How does Java store objects in a List? 如何在Solr上存储java对象 - How to store java objects on Solr Java:存储和访问对象列表的最佳方式 - Java: Best way to store and access a list of objects 在Redis中存储Java对象列表的最佳方法 - Best way to store a list of java objects in Redis Java:使用反射还是将实例对象存储在列表中? - Java: Use reflection or store instance objects in list? 如何在 java class 中最好地存储来自 CSV 的数据? 一个 Row 对象列表,还是一个带有嵌套对象的 object? - How to best store data from CSV in java class? A single list of Row objects, or a single object with nested objects? Spark Task Executors工作时如何在Java并发Java List中存储多个json对象 - How to store multiple json objects in java Concurrent Java List while Spark Task Executors do work 如何在Java中扩展列表以存储其他元数据并强制其仅接受一种类型的对象? - How to extend a list in java to store additional metadata and force it to accept only one type of objects? 如何将多个自定义对象从数组列表存储到Java中的json文件中? - How to store multiple custom objects from an array list into json file in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM