0

I am trying to create an iterator in which I am creating an array of objects. I have to type cast them as Generics array creation is disallowed.

I am getting a run time error

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LRandomizedQueueList$Node;

The full code of the iterator is shown below. I don't know what I am doing wrong.

   private class RandomizedQueueIterator implements Iterator<Item> {
       private Node current = first;
       private Node[] ca = (Node[])new Object[size()];
       //private Node[] ca = new Node[size()];

       private RandomizedQueueIterator() {
           if (first == null) throw new NoSuchElementException();
           for (int j = 0; j < size(); j++) {
               ca[j] = current;
               current = current.next;
           }                        
           StdRandom.shuffle(ca);
           current = ca[0];
       }

       public boolean hasNext()  { return current != null; }

       public Item next() {
           if (current == null) throw new NoSuchElementException();
           Item item = current.item;
           current = current.next; 
           return item;
       }
       public void remove() {
           throw new UnsupportedOperationException("This method is not supported");
       }
   } 

I appreciate any help in understanding this error.

5
  • 1
    Why are you creating it as a new Object[size()]? You can just do new Note[size()]... Commented Oct 19, 2014 at 6:41
  • Because I cannot create an array of generics. Only an array of Object can be created. If I try to do new Node[size()], it gives me an error. Commented Oct 19, 2014 at 6:43
  • Node is not a generic type here. As the exception message indicates, it's a nested class of RandomizedQueueList Commented Oct 19, 2014 at 6:44
  • @RameshManian If Node is a generic type parameter, you should clearly state it in your question. The code you posted doesn't show it. Commented Oct 19, 2014 at 6:45
  • Just posted the full iterator class codde Commented Oct 19, 2014 at 6:54

2 Answers 2

1

Use:

private Node[] ca = new Node[size()];

There is no need to cast the array when you create it. You can just create an array of Node.

Sign up to request clarification or add additional context in comments.

Comments

1

The exception is clear: you can't cast an Object[] to Node[]. Object[] is not a subclass of Node[].

Replace the code by

private Node[] ca = new Node[size()];

3 Comments

This is not allowed in Java. It gives the error; Error: /Users/admin/algs4/assignment2/RandomizedQueueList.java:133: generic array creation
Edit your question and post a complete example reproducing the problem. The exception message indicates that Node is a nested class of RandomizedQueueList, and not a generic type.
Ah, I see. I did not realize that I could not create an array of Node, which is indeed a class inside of RandomizedQueueList.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.