如何存储有关Java中许多人的信息 [英] How to store information about many people in Java

查看:168
本文介绍了如何存储有关Java中许多人的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是在Java中存储人的细节。我查看了Oracle网站主题如何使用表,发现可以使用对象数组(2维)来存储细节。



但我的兴趣是使一个动态对象数组,所以我可以存储任何数量的数据,并且输入

有没有办法可以在java中?如果是这样,你会碰巧知道我可以开始的任何好的地方,也许是一个链接。使用对象数组存储最好的选项?

解决方案

数组保存均匀的数据,即通常数组的每个单元格中的一切类都是一样的。您可能不想在同一类型的字段中存储一个人的姓名和他的鞋子大小,所以我们放弃一个数组的尺寸。您应该声明一个类称为 Person 的类,并在其中指定要为某人存储的所有属性。完成后,您只需要存储一个 Person 的一维数组(或某物)。



接下来,请注意,数组的大小是固定的。如果要扩展数组,则需要分配一个新的更大的数组,将旧的数组复制到新数组中,然后再使用新的数组代替旧数组。



这是很多工作,容易出错。在苹果的修改词中,有一个类就是这样!较旧的合格课程是 Vector ,一个可以存储对象的类,并且每次向其添加新对象时都会增长。现在,为此使用的类(这更有效)是 ArrayList 。同样的事情:你做

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

然后你可以反复

  newPerson =新人(Bruce,Wayne,1972年,高谭市); 
myList.add(newPerson);

,您可以通过执行


$ b $访问列表中的人员b

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

甚至

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






EDIT



确定,为了存储身份证件的人员,如果身份证号码是整数,可以使用身份证号码进行访问,一些适当的代码将是:

  TreeMap<整数,个人> myMap = new TreeMap< Integer,Person>(); 

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

然后您可以使用

  Person p = myMap.get(id); 


My problem is to store details of people in 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.

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).

Is there a way this is possible in 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, i.e. 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. Having done that, you will only be wanting to store a one dimensional array (or something) of 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. Nowadays, the class to use for this (it's a bit more efficient) is 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:

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中许多人的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆