aspose file tools*
The moose likes Beginning Java and the fly likes Can you do this with an ArrayList? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can you do this with an ArrayList?" Watch "Can you do this with an ArrayList?" New topic
Author

Can you do this with an ArrayList?

colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

I you had like a pet shop sim where the user gets and loses lots of dog and cat objects over time so you make an arraylist of type pet and stick all of the dog and cat objects in it and then serialize that. When you deserialize it is there any way you could find out what type the object used to be and then cast the arrays back to that so they could be dog and cats again instead of just pets? Or is there another colletion that can remeber what all of its contents originaly were?

Any help would be appreciated.


www.mormon.org
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
... and then cast the arrays back to that so they could be dog and cats again instead of just pets?


I thought you were putting cats and dogs into the ArrayList. Where did the arrays come from?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

sorry that was a typo. I ment cast the pets back.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18991
    
  40

Originally posted by colton peterson:
sorry that was a typo. I ment cast the pets back.


If it can only be cats or dogs, then check it with the instanceof operator, and cast accordingly.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

Thanks. Now what if one day I expanded the program and add like 20 other types of pets would I have to make a big long else/if stament or is there another way or collection I could use? Also to make the instanceOf work don't I need to override something from object?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18991
    
  40

Originally posted by colton peterson:
Thanks. Now what if one day I expanded the program and add like 20 other types of pets would I have to make a big long else/if stament or is there another way or collection I could use? Also to make the instanceOf work don't I need to override something from object?


Question: When you expand your program with 20 new animal types, will your program have to magically be able to deal with 20 new animals, as those types? Or will it be able to deal with common attributes of those animals, as a common animal type?

If it is the first case, obvious I am being facetious, you have no choice -- you have to deal with the 20 animals anyway, so this is just part of that work.

If you have done it correctly, and create a common animal (or pet) type for you application, then it doesn't matter how many animal types you create. Your program will check to see if it is an instance of animal (or pet), cast it to animal, and deal with it accordingly.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18991
    
  40

Also to make the instanceOf work don't I need to override something from object?


That is also true for casting. You can't cast an object, if the type is just an enum, which holds the type, in your class.

When you mentioned that you want to cast the object, the assumption is that they were different class types.

Henry
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

Okay, so if I made a superclass animal and had all the code I might need in it I could just cast them back to animal? If so what if I needed to put dog specific code in the dog class that extended animal?
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

Also they would not be enums, but if you don't override something from object won't the classes id change after they resart the program and it will not recognize it as a type dog during deserialization?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18991
    
  40

Originally posted by colton peterson:
Okay, so if I made a superclass animal and had all the code I might need in it I could just cast them back to animal? If so what if I needed to put dog specific code in the dog class that extended animal?


If you need dog specific code, then you need to cast it back to dog, which means that you need to check the instance.

My question is.... If you need to add 20 new animals in the future -- What new animal specific feature do you need? especially since these animals don't exist yet? And if you actually have to do it, then you have to cast anyway, so what is wrong with check the instance too?

If you done it correctly, your code should not need to have dog specific code, so your code won't care about about future animal types. And since your code won't have to cast to future animal types, it would need to check the instance of that type either.

Henry
[ November 22, 2007: Message edited by: Henry Wong ]
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

This "program" is not written and I am using it as an example to find out how to do this. So instead lets make it a zoo game. When dogs walk around and find another dog they will behave diffrently than cats walking around and finding another cat. Stuff like that would be the type specific code I need.

And I have no problem with checking the instance of it. I am just wondering if there is any other way to do it than in a huge else/if statment?

Thanks for responding so far.
[ November 22, 2007: Message edited by: colton peterson ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18991
    
  40

Ohhh... By dog specific stuff, I thought you wanted to call a method specific to dogs. This is why it is not a concern to check the instance, as you need to cast it to dog anyway, to call those methods.


To check if two objects are the same animal types is a different story, as you don't need to cast it. To do that, you can get the Class type of the animal, with the getClass() method. With that you can either (1) get the name of the class with getName(), and check the name, or (2) use the isInstance() method to check if two objects are of the same instance -- no need to have a long if/then, as you will be check if an animal is an instance type of another animal instance.

Henry
[ November 22, 2007: Message edited by: Henry Wong ]
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

So would it be something like:

if(myAnimal.getClass().getName() = "dog"){ //this animal is really a dog
dog myDog = (dog) myAnimal
}

That includes casting so I am not sure I understand you.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18991
    
  40

To check if two animals are the exact same type, you can do this... keep in mind, that this doesn't cast the animal. To cast the animal to a specific type, so you can call a specific method, you will need that long if/then/else.



Henry
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

Okay I think I have held you long enough. Thanks it helped a ton.
[ November 22, 2007: Message edited by: colton peterson ]
 
Don't get me started about those stupid light bulbs.
 
subject: Can you do this with an ArrayList?