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.

5
  • 1
    same questions every day.. did you try to read Collections guide or look through generics tag here? Commented Apr 13, 2013 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 Commented Apr 13, 2013 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 Commented Apr 13, 2013 at 21:17
  • "but giving me runtime errors about raw types" There are no "runtime errors about raw types". Everything is raw at runtime. Commented Apr 13, 2013 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? Commented Apr 15, 2013 at 3:44

3 Answers 3

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>());
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not even sure how to instantiate that, could you give me an example? ArrayList<LinkedList<String>> list = new ArrayList<LinkedList<String>>() ??
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
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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.