12

I was wondering if its possible to directly cast objects within a foreach loop.

We have the following two classes were one extends the other:

class Book {};
class ExtendedBook extends Book {};

Now we have an array of Books which I want to loop through, because its a ExtendedBook search I'm sure that all Books are actually Extended Books. Is there a way to directly cast them?

Book [] books = bookSearch.getBooks("extendedBooks");

for (Book book: books){
   ExtendedBook eBook = (ExtendedBook) book;
   ....
}

This involves two steps. First looping through the books and at the second step casting them. Can one do it in one step?

What doesn't work:

// Directly assign it to a different type
for (ExtendedBook book : books){}

// Directly casting the array
ExtendedBooks [] eBooks = (ExtendedBooks []) books;

// Same goes for trying both in one step
for (ExtendedBook book : (ExtendedBook []) books){}

I know its not a real pain, but keeping the loop shorter would be nice and maybe more readable as you save a dummy variable, which is just used for casting instead of the actual action.

1
  • 2
    It appears that books is an array of Book not ExtendedBook. The elements of the array might be ExtendedBook, but you can only change this for each element. btw you can unbox in a loop Long[] array = ... for(long l: array) { } Jun 15, 2012 at 11:57

5 Answers 5

3

How about using Generics?

Write your getBooks signature as:

<B extends Book> B [] getBooks(Class<B> bookType)

Now, if you want to search for books of the type ExtendedBook, just call:

ExtendedBooks [] eBooks = bookSearch.getBooks(ExtendedBook.class)

No typecasting or other unsafe stuff needed. Nice and clean.

Of course you still have to make sure that only ExtendedBook only returns that kind of book, but it looks like you solved that already.

1
  • Actually I'm working with web services and have a generated client that returns the arrays.
    – Udo Held
    Jun 15, 2012 at 14:40
2

I'm reasonably sure you cannot cast in the loop as you would like.

1

Think obout casting

ExtendedBook ex=(ExtendedBook)new Book();

It is accepted by compilator but JVM throws java.lang.ClassCastException, because that type of casting is wrong -> Book is not ExtendedBook, so there is a chance that it wont handle potential new methods added in ExtendedBook class.

For the same reason you can't do something like

ExtendedBook[] exbooksB=(ExtendedBook[]) new Book[10];

but can

Book[] booksA=new ExtendedBook[10];
ExtendedBook[] exbooks=(ExtendedBook[]) booksA;
0

You cannot because java does not support implicit conversion overloading like in C# with the implicit operator.

0

You can do this.

Iterables.filter('Your array', the type class).forEach(x -> { } }

Iterables.filter(data, JSONObject.class).forEach(x -> { 
    x.getString("something") });    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.