0

Why are

LinkedList<String>[] list = new LinkedList[32];

StaticError:Bad types in assignment:from raw LinkedList[] to LinkedList<String>[]

And

LinkedList<String>[] list= (LinkedList<String>[]) new LinkedList[32];
StaticError: Bad types in cast: from raw LinkedList[] to LinkedList<String>[]

both compiling but giving me runtime errors about raw types?

I need to create an array of LinkedLists (basically for a hashtable...and it's homework so I can't deviate too much from it). I was planning on handling the collisions by using separate chaining, that is just filling repeated entries in the array with sequential elements in the LinkedList. Any thoughts would be appreciated.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
SGWF
  • 51
  • 1
  • 3
  • 10
  • 1
    same questions every day.. did you try to read Collections guide or look through generics tag here? – Vitaly Apr 13 '13 at 21:06
  • did you not notice that my code is the exact same as in the thread you just referenced? and yes I did my best to read up on the subjet, but it seemed like a relatively small error, which it was, and NPE did an excellent job of at least pointing me in the right direction – SGWF Apr 13 '13 at 21:14
  • Ok, I see that now, that the accepted answer of that thread has the same thing, but I skimmed past it because it had half the votes of the answer about casting the array to LinkedLists, my bad I guess, it's not always easy to tell when things start failing – SGWF Apr 13 '13 at 21:17
  • "but giving me runtime errors about raw types" There are no "runtime errors about raw types". Everything is raw at runtime. – newacct Apr 13 '13 at 22:14
  • well, I dunno what you call the interactions pane, I was just trying to say that I get that error when I run them, Bad types then? – SGWF Apr 15 '13 at 03:44

3 Answers3

5

In Java, arrays and generics don't play along all that nicely.

I know you say you can't deviate from this design too much. A slight deviation that would make your life a lot easier would be to use an ArrayList instead of an array:

    List<LinkedList<String>> buckets = new ArrayList<LinkedList<String>>();
    for (int i = 0; i < num_buckets; ++i) {
        buckets.add(new LinkedList<String>());
    }
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • I'm not even sure how to instantiate that, could you give me an example? ArrayList> list = new ArrayList>() ?? – SGWF Apr 13 '13 at 21:03
  • ah, that does it, I guess I could do that, I was going off some old posts I on SO that talked about arrays of linkedlists, but I guess I'll just forget about them for now – SGWF Apr 13 '13 at 21:06
1

This is due to the issue of 'erasure' (as you say it's "homework" I would strongly encourage you to read up on the subject: you'll learn fascinating stuff!) and essentially relates to the fact that when they introduced generics, they had to maintain compatibility with existing non-generic code - at runtime.

So, the two answers (which appeared as I was typing this one) would solve your problem (in particular, adding the notation to the constructor invocation) but would not really address the fundamental issue (that arrays and generics don't really like each other).

Using an ArrayList<String> would be preferable.

Marco Massenzio
  • 2,822
  • 1
  • 25
  • 37
0

This code:

import java.util.LinkedList;

public class ArrayOfLinkedLists {

   public static void main( String[] args ) {
      @SuppressWarnings("unchecked")
      LinkedList<String>[] array = new LinkedList[32];
      array[0] = new LinkedList<>();
      array[0].add( "Hello" );
      array[0].add( ", " );
      array[0].add( "World!" );
      for( LinkedList< String > l : array ) {
         if( l != null ) {
            for( String s : l ) {
               System.out.println( s );
            }
         }
      }
   }
}

outputs:

Hello
, 
World!

Your code works.

Aubin
  • 14,617
  • 9
  • 61
  • 84