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??

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)

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??

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 ???

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??

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.

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??

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.