| | |
Arraylist Problem Help Please
![]() |
•
•
Join Date: Oct 2008
Posts: 29
Reputation:
Solved Threads: 0
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??
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.
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 !!!
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 ???
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 ?"
"How to ask questions the smart way ?"
•
•
•
•
pet is split into 4 sections
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 ?"
"How to ask questions the smart way ?"
•
•
•
•
this is for my project i have to create a gui for a pet store
•
•
•
•
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??
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 ?"
"How to ask questions the smart way ?"
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: XML file not found for parser?
- Next Thread: Recursion help
| Thread Tools | Search this Thread |
911 addball addressbook android api applet application apps array automation awt binary bluetooth businessintelligence busy_handler(null) button card class client code collision component constructor crashcourse css csv database draw eclipse ee error eventlistener exception fractal free game gis givemetehcodez graphics gui html ide image integration j2me japplet java javaarraylist javadoc javafx javamicroeditionuseofmotionsensor javaprojects jni jpanel jtree julia jvm linux loan map method migrate mobile netbeans objects oracle oriented phone physics plazmic problem program programming project projects radio recursion replaydirector reporting scanner se server service set sharepoint smart sms software sortedmaps sql swing test textfield threads tree trolltech ubuntu unlimited utility windows






