[英]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存储工作人员列表的最便捷方法是:
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.