943,682 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 728
  • Java RSS
Dec 14th, 2008
0

Arraylist Problem Help Please

Expand Post »
sorry to bother all of you again but i was just wondering if i can use the same arraylist more than 1 time.

this is for my project i have to create a gui for a pet store and a pet can be either a : mammal,reptile,fish or bird.
so basically i want to create a array list which will hold all of these pets but i only want to create the array list once and then get it to add a to that list from different areas.

i hope i explained this well enough for everyone to understand all help is much apperciated
thanks

and cause this is a gui can i declare the array list when my program starts and then refer back to the arraylist??
Last edited by Superstar288; Dec 14th, 2008 at 12:24 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
Superstar288 is offline Offline
46 posts
since Oct 2008
Dec 14th, 2008
0

Re: Arraylist Problem Help Please

What you can do is you can make a class Pet and then make all the other classes extend from this class such as the ones you mention above. Then when you create an ArrayList of Pet type it will be able to hold objects of all types that have 'Pet' class in their parental hierarchy. (all classes that derive from the Pet class either directly or indirectly)
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Dec 14th, 2008
0

Re: Arraylist Problem Help Please

hey thats what i have done so far im just not sure on where to declare the first arraylist as it actually for the gui could i just it declare it to the actual pet clases with all my getters and setters??
Reputation Points: 10
Solved Threads: 1
Light Poster
Superstar288 is offline Offline
46 posts
since Oct 2008
Dec 14th, 2008
0

Re: Arraylist Problem Help Please

Whenever you are trying to add a variable to a Class always check for a "HAS-A" relationship.

For ex: If you were to declare a Car class, it would definitely have the 4 tyre variables. Because A Car HAS Tyres.

So you are talking of adding the ArrayList to your Pet class, But does "a Pet HAS ArrayList (which is a Collection of Pets)" sound logical to you ???
Last edited by stephen84s; Dec 14th, 2008 at 2:23 pm.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Dec 14th, 2008
0

Re: Arraylist Problem Help Please

hey i kind of understand what you mean stephen well at the momment a pet is split into 4 sections that i stated above all i wish to do is when i add a pet such as a mammal it will add it into a arraylist called pets. then i want to re-use the same arraylist so i can add a reptile etc??
Reputation Points: 10
Solved Threads: 1
Light Poster
Superstar288 is offline Offline
46 posts
since Oct 2008
Dec 14th, 2008
0

Re: Arraylist Problem Help Please

Quote ...
pet is split into 4 sections
What you are referring to is called the IS-A relationship.
ex:
A mammal IS-A pet.
A reptile IS-A pet.
Better known as Inheritance. I suppose your reptile, mammal etc are extending the Pet class.

Now in case you are looking where to put the ArrayList of Pets, check if in your Class structure you have any class resembling PetShop, the ArrayList would go there.

So then you can say the PetShop HAS-AN ArrayList of Pets that is the PetShop HAS-A Collection of Pets.

Got to call it a night here, bye for now.
Last edited by stephen84s; Dec 14th, 2008 at 2:52 pm.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Dec 14th, 2008
0

Re: Arraylist Problem Help Please

oh ok i understand you now yes i havent got a petshop class at the momment. so if i was to do this would it make much of a difference as i was doing a gui??
Last edited by Superstar288; Dec 14th, 2008 at 3:18 pm.
Reputation Points: 10
Solved Threads: 1
Light Poster
Superstar288 is offline Offline
46 posts
since Oct 2008
Dec 15th, 2008
0

Re: Arraylist Problem Help Please

Quote ...
this is for my project i have to create a gui for a pet store
You were creating a GUI for a Pet Store, so chances are you have have that class but haven't noticed it.

oh ok i understand you now yes i havent got a petshop class at the momment. so if i was to do this would it make much of a difference as i was doing a gui??
Well if you do not have anything resembling PetShop or PetStore, then I would actually recommend you create a new class by that name and assign it the appropriate responsibility (methods and attributes).
For example variables in your PetStore / PetShop class could be like StoreKeeper (A Pet Store has a Store Keeper) , ArrayList of Pets, etc.
Methods that you can have there include methods for adding pets to the ArrayList (to indicate new arrivals) / removing methods from the ArrayList to indicate Sale of Pets, Modifying the Store Keeper attribute, etc.

Now let us talk about your GUI, According to me you should separate out your GUI from your core logic which performs the processing. For example if tomorrow you have to switch to using the Console for running your program, if your code is too tightly coupled with your GUI you may have to rewrite your entire code.

On the other hand if your processing logic is in separate classes, and you have made appropriate methods available for performing a given task to the GUI classes, if you have change your GUI from say Swing to AWT or maybe skip the grphical UI altogether and switch to the Console, all you would have to do is remove the GUI code and the rest of your project could still be used without any issues (as log as there are no changes in the core logic).

So the answer to your question is "no", it shouldn't make too much of a difference.

Hope this makes my point clear.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: XML file not found for parser?
Next Thread in Java Forum Timeline: Recursion help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC