Results 1 to 3 of 3
  1. #1
    Umi is offline Member
    Join Date
    Sep 2009
    Rep Power

    Default ClassCastException

    Java Code:
    public static Object[] extendArraySize(Object[] o) {
            Object[] extended = new Object[o.length + 1];
            System.arraycopy(o, 0, extended, 0, o.length);
            return extended;
    Java Code:
    Renamer[] renamer = new Renamer[4];
    renamer = (Renamer[]) extendArraySize(renamer);
    When running this, I get: Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lrenamefiles.Renamer;

    Why do I get this?

    Also, is it possible to pass a method as a parameter for another method?
    For example in the use for calculating integrals.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    You can't cast arrays like that. This looks like a situation where generics might help.

    Better still: don't use arrays but rather the more flexible ArrayList which is already "genericized".

    Edit: generics don't work well with arrays in that you can't create a generic array since type information doesn't exist at run time. You're far better off using an ArrayList here.
    Last edited by Fubarable; 01-13-2010 at 08:53 PM.

  3. #3
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Stuttgart, Germany
    Rep Power


    your second question is can be solved by using interfaces
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

Similar Threads

  1. ClassCastException: in EJB3
    By nida in forum Advanced Java
    Replies: 2
    Last Post: 07-03-2009, 03:03 PM
  2. ClassCastException
    By paulsim in forum Java Applets
    Replies: 2
    Last Post: 08-21-2008, 02:14 PM
  3. ClassCastException in TreeSet
    By pHew in forum New To Java
    Replies: 2
    Last Post: 01-16-2008, 12:20 AM
  4. ClassCastException
    By Ed in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 05:26 AM
  5. ClassCastException
    By Felissa in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 05:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts