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
Personand 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) ofPerson.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) isArrayList. Same thing: You doand then you can repeatedly
and you can access folks in the list by doing
or even
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:
then you can retrieve it with