-1

I was wondering if you can cast an object[] to another object[] of a different class.

Here's what I tried doing, but clearly it isn't working.

public CardCollection() {
    CardCollection[] myCollection = new CardCollection[MAX_CARDS];
    for(int i = 0; i < myCollection.length; i++){
        myCollection[i] = (BaseballCard[]) Collection[i];
    }
}

I'm getting an error when I tried casting the collection Object[] to BaseballCard.

I had previously made a

BaseBallCard[] Collection = new BaseBallCard[MAX_CARDS]; // MAX_CARDS=100;

I have tried myCollection[i]= (BaseballCard) Collection[i]. Gave me an error saying BaseballCard cannot be resolved to a type.

3 Answers3

1

what you are doing

 myCollection[i] = (BaseBallCard[]) Collection[i];

what you should do

 myCollection[i] = (BaseBallCard) Collection[i];

converting each object one by one

Condition required

You can cast to Object type and then again cast it to some different type of Object based on some conditions (like Object.getClass()). Means you should have same Object1.getClass().getName() and Object2.getClass().getName()

Source1 Source2

Community
  • 1
  • 1
singhakash
  • 7,613
  • 4
  • 25
  • 59
1

If BaseBallCard is a subclass of CardCollection, your code can be corrected as follows:

public CardCollection() {
    CardCollection[] myCollection = new CardCollection[MAX_CARDS];
    for(int i = 0; i < myCollection.length; i++){
        myCollection[i] = (BaseBallCard) Collection[i]; // note BaseBallCard not [i]
    }
}

Or more easily using Arrays.copyOf():

public CardCollection() {
    CardCollection[] myCollection = Arrays.copyOf(Collection, MAX_CARDS, BaseBallCard.class);
}

But if BaseBallCard is a subclass of CardCollection, then every time you create a BaseBallCard, you'll implicitly call this constructor (since it is the default constructor for the parent type) and you'll wind up creating a new array of CardCollection every time you make a BaseBallCard. You don't really want that, believe me.

What you're probably trying to do, given the naming of the classes, is store a BaseBallCard[] in CardCollection.

public CardCollection() {
    BaseBallCard[] myCollection = new BaseBallCard[MAX_CARDS];
    // just create the array - don't try to populate it in the constructor
}

Then you could have separate methods for adding and removing a BaseBallCard from the CardCollection.

Michael Myers
  • 178,094
  • 41
  • 278
  • 290
  • Oh. So all i had to do was extends CardCollection for BaseballCard class. nice. And as for the Arrays.copyof(). I can't use it. Apparently, the hw would be too easy if i did that. So we have to do everything manually. Professors and their limitations. QQ.. But that fixes everything. O_O – user3083893 Feb 09 '15 at 16:46
  • Please read the second half of my answer; you almost certainly *don't* want to make BaseBallCard extend CardCollection and use the code from your question. – Michael Myers Feb 09 '15 at 16:50
  • I did make a object[] of type BaseBallCard, whenever i try to use a method in the CardCollection Class, it wouldn't work. , } – user3083893 Feb 09 '15 at 16:53
0

In your specific case, you can achieve what you want as this:

public CardCollection() {
    CardCollection[] myCollection = new CardCollection[MAX_CARDS];
    for (int i = 0; i < myCollection.length; i++){
        myCollection[i] = (BaseBallCard) Collection[i];
    }
}

You might even skip the explicite cast, as Collection is a BaseBallCard[].

meskobalazs
  • 14,510
  • 2
  • 33
  • 55