views:

73

answers:

3

Hi, 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 ?

Thanks a million

+2  A: 

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

TofuBeer
A: 

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.

venJava
+3  A: 

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);
Carl Smotricz
going to try this
Haxed
Ok this seems like a good idea, however I want to be able to use an say for instance a primary key (like person id) in a record to retrieve all the details(apparently can be done by LinkedList inclusive of the person id) and also retrieve certain details like age and sex via the person id (possible with the Map interface). Is there an interface to perform both these operations ?
Haxed
Map interface i was referring to is the TreeMap
Haxed