Casting Object Type in Collection

I need to cast a collection of objects to a subclassed object, but continue to receive an ArrayStoreException.  

For Example, the objects in the collection are all of a Dog type.  I'd like to cast the complete collection to a subclass I created called Dalmation that extends Dog...

//example code
List c = new java.util.ArrayList();
//...add collection of 'Dog' objects to c -> c.addAll(...);
c.toArray (new Dalmation[0]);

I thought this would work, however it doesn't.
What's the proper way to cast these objects in the collection?
sapientconceptionsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
Dalmation[] d = (Dalmation[]) c.toArray (new Dalmation[0]);


0
objectsCommented:
thats assuming they are all Dalmations in your collection, if they are actually Dog's use:

Dog[] d = (Dog[]) c.toArray (new Dog[0]);
0
sapientconceptionsAuthor Commented:
They are actually Dog's, however I'm trying to cast to my new object type, which extends dog.  So (Dalmation[])c.toArray(new Dalmation[0]) *should* work?  

Why exactly would I get an ArrayStoreException?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

objectsCommented:
because it tries to store an object of type Dog in an array of Dalmatioons
0
objectsCommented:
> They are actually Dog's, however I'm trying to cast to my new object type, which extends dog.  
> So (Dalmation[])c.toArray(new Dalmation[0]) *should* work?  

No, because a Dog is not a Dalmation so u can't store a Dog in an array of Dalmations.

0
sapientconceptionsAuthor Commented:
Ok, I understand that.  Thanks for the explanation.

So, how can I 'wrap' the dog object in the Dalmation object?  I need to extend the Dog class somehow...
0
sapientconceptionsAuthor Commented:
i believe it's called downcasting right?
0
objectsCommented:
You need to get an array of dogs, and cast individual elements to Dalmation (if they are Dalmataions)

eg.

Dog[] d = (Dog[]) c.toArray (new Dog[0]);
for (int i=0; i<d.length; i++)
{
   if (d[i] instanceof Dalmation)
   {
     Dalmation dal = (Dalmation) d[i];
     // do whatever with the dalmation
   }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sapientconceptionsAuthor Commented:
Thanks for your help Objects.

Basically none of the elements will be of type Dalmation.  So in order for me to have a Dalmation object, I have to create a new instance of Dalmation and pass all the instance variables back through?  The problem I'm encountering is the super class may have a ton of variables...I was hoping there was some way to avoid having to do something like this:

//sample Dog class
public class Dog {
  private hasSpots = false;
  private isBigDog = false;

 public Dog(boolean hasSpots, boolean isBigDog) {
  this.hasSpots = hasSpots;
  this.isBigDog = isBigDog;
 }

 public boolean isBigDog() {
   return false;
 }

 public boolean hasSpots() {
   return false;
 }
}

//sample Dalmation class
public class Dalmation extends Dog {
  public Dalmation(boolean hasSpots, boolean isBigDog) {
   super (hasSpots, isBigDog);
 }

//additional dalmation methods...
}

0
objectsCommented:
> o in order for me to have a Dalmation object, I have to create a new instance of Dalmation and pass all the instance variables back through?

thats correct.
A subclass ctor must call one of its parent classes ctor's.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.