简体   繁体   中英

How to store information about many people in Java

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, 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. 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.util.List,java.util.Map和System.arraycopy中查找一些可以帮助您的数据结构。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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