How to store information about many people in Java

微笑、不失礼 提交于 2020-01-25 12:22:51

问题


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?


回答1:


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



回答2:


Look at java.util.List, java.util.Map, and System.arraycopy for some data structures that will help you.




回答3:


First you should understand that the Object 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.



来源:https://stackoverflow.com/questions/3217989/how-to-store-information-about-many-people-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!