简体   繁体   English

如何在Java中存储有关许多人的信息

[英]How to store information about many people in Java

My problem is to store details of people in Java. 我的问题是用Java存储人员详细信息。 I looked up at the Oracle website topic How to Use Tables and found out that one can use object arrays(2 dimensional) to store details. 我查看了Oracle网站主题“ 如何使用表” ,发现可以使用对象数组(二维)存储详细信息。

But my interest is to make a dynamic object array so I can store any amount of data and also take input to store those details from the user. 但是我的兴趣是制作一个动态对象数组,这样我可以存储任意数量的数据,也可以接受用户输入来存储这些详细信息。 For instance, at the beginning of the program I can specify an object array to hold 5 records, but I actually want an array that could add another available location if I need to add more records (dynamic). 例如,在程序开始时,我可以指定一个对象数组来保存5条记录,但是实际上我想要一个可以在需要添加更多记录(动态)的情况下添加另一个可用位置的数组。

Is there a way this is possible in java ? 有没有办法在Java中做到这一点? If so, do you happen to know any good places I could start, perhaps a link. 如果是这样,您碰巧知道我可以开始的任何好地方,也许是链接。 Is storing using an Object array the best option? 使用对象数组存储是最佳选择吗?

An array holds homogenous data, ie usually the class of everything in every cell of an array is the same. 数组保存同质的数据,即通常数组中每个单元的所有类都是相同的。 You will likely not want to store a person's name and his shoe size in fields of the same type, so... let's drop one of the array's dimensions. 您可能不希望在同一类型的字段中存储一个人的名字和他的鞋子大小,所以...让我们删除数组的一个维度。 You should declare a class called something like Person and specify within it all the attributes you want to store for a person. 您应该声明一个名为Person类的类,并在其中指定要为一个人存储的所有属性。 Having done that, you will only be wanting to store a one dimensional array (or something) of Person . 完成此操作后,您将只想存储Person的一维数组(或其他东西)。

Next, note that arrays are fixed in size. 接下来,请注意数组的大小是固定的。 If you want to extend an array, you would need to allocate a new, bigger array, copy the contents of the old one into the new one and then go on working with the new one in place of the old one. 如果要扩展数组,则需要分配一个更大的新数组,将旧数组的内容复制到新数组中,然后继续使用新数组代替旧数组。

That's a lot of work, and error prone. 这需要大量工作,而且容易出错。 In the modified words of Apple, there's a class for that! 用苹果的修饰词来说,有一个类! The older qualified class was Vector , a class where you could store objects and that would grow every time you add a new object to it. 较早的合格类是Vector ,您可以在其中存储对象的类,并且每次向其添加新对象时该类都会增长。 Nowadays, the class to use for this (it's a bit more efficient) is ArrayList . 如今,用于此的类(效率更高)是ArrayList Same thing: You do 同样的事情:你做

ArrayList<Person> myList = new ArrayList<Person>();

and then you can repeatedly 然后你可以反复

newPerson = new Person("Bruce", "Wayne", 1972, "Gotham City");
myList.add(newPerson);

and you can access folks in the list by doing 您可以通过以下方式访问列表中的人员

int personNumber = 0;
Person retrievedPerson = myList.get(personNumber);

or even 甚至

for (Person someone : myList) {
   System.out.println(someone);
}

EDIT 编辑

OK, to store people with an ID and access them by that ID assuming the person ID is an integer, some appropriate code would be: 好的,要存储具有ID的人员并假设该人员ID是整数,则可以通过该ID对其进行访问,则一些合适的代码应为:

TreeMap<Integer, Person> myMap = new TreeMap<Integer, Person>();

newPerson = ...
myMap.put(newPerson.getId(), newPerson);

then you can retrieve it with 然后您可以使用

Person p = myMap.get(id);

在java.util.List,java.util.Map和System.arraycopy中查找一些可以帮助您的数据结构。

First you should understand that the Object array passed to the JTable won't get stored . 首先,您应该了解Object array passed to the JTable won't get storedObject array passed to the JTable won't get stored It just be used to display the records of what you've done. 它仅用于显示您所做的记录 To store the values you've got, you have to use Files or DB. 要存储您拥有的值,您必须使用文件或数据库。

You can set Object array size dynamically in a way that you should know how many records you really need to show . 您可以通过动态设置对象数组大小的方式来知道真正需要显示多少条记录 I think with JDK 1.6 you can't dynamically change the size of the Array, but the upcoming JDK 7 may provide such a feature. 我认为使用JDK 1.6不能动态更改Array的大小,但是即将发布的JDK 7可能会提供这样的功能。

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

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