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

    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
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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 09:53 PM.

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

    Default

    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, 04:03 PM
  2. ClassCastException
    By paulsim in forum Java Applets
    Replies: 2
    Last Post: 08-21-2008, 03:14 PM
  3. ClassCastException in TreeSet
    By pHew in forum New To Java
    Replies: 2
    Last Post: 01-16-2008, 01:20 AM
  4. ClassCastException
    By Ed in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 06:26 AM
  5. ClassCastException
    By Felissa in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 06: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
  •