Arraylist Problem Help Please

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 29
Reputation: Superstar288 is an unknown quantity at this point 
Solved Threads: 0
Superstar288 Superstar288 is offline Offline
Light Poster

Arraylist Problem Help Please

 
0
  #1
Dec 14th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Arraylist Problem Help Please

 
0
  #2
Dec 14th, 2008
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)
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 29
Reputation: Superstar288 is an unknown quantity at this point 
Solved Threads: 0
Superstar288 Superstar288 is offline Offline
Light Poster

Re: Arraylist Problem Help Please

 
0
  #3
Dec 14th, 2008
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??
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Arraylist Problem Help Please

 
0
  #4
Dec 14th, 2008
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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 29
Reputation: Superstar288 is an unknown quantity at this point 
Solved Threads: 0
Superstar288 Superstar288 is offline Offline
Light Poster

Re: Arraylist Problem Help Please

 
0
  #5
Dec 14th, 2008
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??
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Arraylist Problem Help Please

 
0
  #6
Dec 14th, 2008
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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 29
Reputation: Superstar288 is an unknown quantity at this point 
Solved Threads: 0
Superstar288 Superstar288 is offline Offline
Light Poster

Re: Arraylist Problem Help Please

 
0
  #7
Dec 14th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Arraylist Problem Help Please

 
0
  #8
Dec 15th, 2008
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.

Originally Posted by Superstar288 View Post
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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC