Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: having problem declaring a generic type array in OrderSet class

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default having problem declaring a generic type array in OrderSet class

    guys, I have declare a generic array in OrderSet class; however, during compilation is giving me an error:

    Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
    	at wordset.OrderSet.<init>(OrderSet.java:17)
    	at wordset.Main.main(Main.java:20)
    Java Result: 1

    main

    public class Main {
     
        public static void main(String[] args) throws IOException
        {
            OrderSet<String> s1 = new OrderSet<>();
     
            String fileName;
            Scanner in = new Scanner(System.in);
     
            System.out.println("Enter the name of the input file");
            fileName = in.next();
     
            File inFile = new File(fileName);
     
            Scanner input;
            try {
                input = new Scanner(inFile);
                while (input.hasNext()) 
                {
                    String w = input.next();
                    s1.add(w);                
     
                }
            } catch (FileNotFoundException ex) {
               Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);            
            }
     
            s1.show();
     
        } 
     
    }

    public interface Set<T> {
     
        public void add(T x);
     
        public void show();
     
    }


    import java.util.Arrays;
     
     
    public class OrderSet<T extends Comparable> implements Set<T> {
     
        private T[] items;
        private int size;
     
        public OrderSet()
        {
            items = (T[]) new Object[5];        
        }
     
        @Override
        public void add(T s)
        {
     
            if(size >= items.length)
            {
                items = grow(items);
            }
     
            for (int i = 0; i < items.length; i++) {
                if(items[i] == null)
                {
                    items[i] = s;
                    size++;
                    break;
                }
            }      
     
        }
     
        @Override
        public void show()
        {
            int i = 0;
            while(i < size)
            {            
                System.out.print(items[i]+", ");
                i++;
            }
        }
     
        public T[] grow(T[] a)
        {
            T[] newA = (T[]) new Object[a.length+5];
            System.arraycopy(a, 0, newA, 0, a.length);
            return newA;
        }
     
    }
    Last edited by mia_tech; July 10th, 2012 at 12:30 PM.


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: having problem declaring a generic type array in OrderSet class

    I don't understand that error, but I can tell you that this isn't correct:

    OrderSet<String> s1 = new OrderSet<>();

    That you can't use empty generic parameter brackets. Better to do:


    OrderSet<String> s1 = new OrderSet<String>();

    Edit: Please ignore this drivel as it was posted in ignorance. Listen to helloworld922.
    Last edited by Fubarable; July 10th, 2012 at 06:42 PM.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: having problem declaring a generic type array in OrderSet class

    Quote Originally Posted by Fubarable View Post
    I don't understand that error, but I can tell you that this isn't correct:

    OrderSet<String> s1 = new OrderSet<>();

    That you can't use empty generic parameter brackets. Better to do:


    OrderSet<String> s1 = new OrderSet<String>();
    That's actually a new featured of Java 7 (type inferencing for generic object creation). See: Type Inference for Generic Instance Creation

    The problem is actually here:

    items = (T[]) new Object[5];

    You can't cast an Object[] to a T[] since Object is not a T or a subclass of T.
    Last edited by helloworld922; July 11th, 2012 at 02:34 AM.

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    Fubarable (July 10th, 2012)

Similar Threads

  1. array element declaring problem
    By jack_nutt in forum Collections and Generics
    Replies: 11
    Last Post: August 1st, 2011, 06:29 PM
  2. Creating array from generic type objects?
    By AndrewMiller in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2010, 09:22 PM
  3. <..> when declaring a class
    By Asido in forum Java Theory & Questions
    Replies: 1
    Last Post: September 8th, 2010, 08:47 AM
  4. generic class
    By fireatmuself in forum Collections and Generics
    Replies: 4
    Last Post: November 17th, 2009, 06:06 AM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM