Link to home
Start Free TrialLog in
Avatar of weaverk
weaverkFlag for Switzerland

asked on

Simple Java question - Arraylist or multidimensional array

I am new to java and trying to figure out this problem:
I have 2 classes, Pets and PetOwners


I am looking for a suitable way to store this data temporarily, the variables are known (name etc) but the number of entries is unknown till runtime.
I am unsure if it is better to make a multidimensional array to store this info, or to use arraylists of arrays (if this is possible!)

Example data:
Name,    age, 	 number of pets
John,    15,     3
Jack,     66,    4
Jill,     3,    7
…	…	…
Etc

Open in new window



In Class Pets I create an object of PetOwners Class  called X

I need to be able to apply the below methods to whatever storage mechanism I use:
As well as to be able to add /remove specific ones.
public void addOwner(PetOwners X)  {}
public boolean isOwner(PetOwners X) {}

any Advice about which data store would be most suitable and how best to implement it  is much appreciated.
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

I am unsure if it is better to make a multidimensional array to store this info, or to use arraylists of arrays (if this is possible!)

Neither arrays or arraylists of arrays are the optimal solutions, In fact they are not object-oriented.
If you already have classes to store your data (PetOwner), you can store them into a typed list (http://docs.oracle.com/javase/6/docs/api/java/util/List.html).

List<PetOwner> myList = new ArrayList<PetOwner>();

Lists allows dynamic growing as you require.
Avatar of CEHJ
List<PetOwner> myList = new ArrayList<PetOwner>();

Open in new window

You can put that into the Pet class possibly but in terms of design, it's better not to tightly couple. One way of doing that would be to introduce a third class
ASKER CERTIFIED SOLUTION
Avatar of basav_com
basav_com
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of weaverk

ASKER

thank you for the explanation!