Results 1 to 5 of 5
Like Tree2Likes
  • 2 Post By Tolls

Thread: Casting Enum Type arrays to object type arrays

  1. #1
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Unhappy Casting Enum Type arrays to object type arrays

    I have this scenario:
    Java Code:
    // File: Fruits.java
    package fruits;
    public enum Fruits {
          APPLE('A'),
          MANGO('M'),
          ORANGE('O');
          
          public Fruits(char value) {
                this.value = value;
          }
          private final value;
         
          public char getValue() { return value;}
    }
    // File: ArrayUtils.java
    package utils;
    public Class ArrayUtils {
    
          public static <T> T [] trimArray(T [] array) {
              ArrayList<T> trimmed = new ArrayList<>();
              for (T object: array)
                  if (object != null)
                     trimmed.add(object);
              return (T[])trimmed.toArray();
          }
         
          public static <E extends Enum> E [] trimEnumArray(E [] array) {
            ArrayList<Enum> trimmed = new ArrayList<>();
            for (Enum object: array)
                if (object != null)
                    trimmed.add(object);
            return (E[]) trimmed.toArray(new Enum [] {null}); 
            // I hope its safe to do this          ^^^     , refer to javadoc
        }
    }
    
    // File: EnumUtils.java
    package utils;
    public class EnumUtils {
          
          public static Fruit fruitOfValue(char value) {
                  for(Fruit fruit: Fruit.values())
                      if (fruit.getValue() == value)
                           return fruit;
                  return null;
          }
    }
    
    public class TestEverything {
           private Fruit [] availableFruits;
           private char fruitValues [] = {'A','O','N','V');
    
           public void setAvailbleFruits()
                availableFruits = new Fruit[fruitValues.length];
                for(int i = 0; i < fruitValues.length; i++)
                    availableFruits[i] = EnumUtils.getFruitOfValue(fruitValues[i]);
                availableFruits = ArrayUtils.trimEnumArray(availableFruits);  // FIXME: HERE
          }
    
          public Fruit [] getAvailableFruits() {
                  return availableFruits;
          }
    }
    Now, if you see the line containing FIXME: HERE, that's where a problem arises.I would get an exception like:
    Java Code:
    java.lang.ClassCastException: [Ljava.lang.Enum; cannot be cast to [Lfruits.Fruit;
    If you might help, I'll appreciate.
    Last edited by nmvictor; 02-16-2012 at 10:22 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: Casting Enum Type arrays to object type arrays

    java.lang.ClassCastException: [Ljava.lang.Enum; cannot be cast to [Lfruits.Fruit;
    That makes sense. An Enum if not a Fruit object? Why do you think you can cast it?
    What is it you are trying to do?


    The posted code is FULL of compiler errors. You needed to fix them before you post this.
    Last edited by Norm; 02-16-2012 at 02:04 PM.

  3. #3
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Default Re: Casting Enum Type arrays to object type arrays

    You are not helping with you're reply. I know the compiler error makes sense, I have met many before and dealt with them, what I dont know is how to deal with this one, which is why I posted. Sometimes if you are not going ton help, then not replying to someones post is the mature thing to do.
    Why do you think you can cast it?
    Well, call me a fool if you wish, its because I can down cast the class Object to a String or upcast a String to an Object because the class String extends the class Object. The enum Fruit on the other hand extends the class Enum<E>, so I expected a similar result on casting but got an error instead. So share your smart thinking with the rest of us and help me figure this out, isn't that what Open source is all about?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Casting Enum Type arrays to object type arrays

    Oh dear.
    Have you been reading "How To Win Firends and Influence People"?
    DarrylBurke and Everyman like this.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: Casting Enum Type arrays to object type arrays

    Please correct the many compiler errors and post the code again with only the casting error.
    For example where is the class Fruit defined?

Similar Threads

  1. Replies: 10
    Last Post: 06-26-2011, 01:07 AM
  2. Help with type casting arrays
    By javaman1 in forum New To Java
    Replies: 3
    Last Post: 02-01-2011, 03:44 AM
  3. Setting values from One Enum type to another enum type.
    By reach2sudhakar in forum New To Java
    Replies: 3
    Last Post: 09-23-2010, 06:02 PM
  4. Casting Object to another type
    By green_river48 in forum New To Java
    Replies: 12
    Last Post: 04-03-2010, 10:52 AM
  5. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 06:24 PM

Tags for this Thread

Posting Permissions

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