Results 1 to 4 of 4
  1. #1
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default 2D array of ArrayList (technically a 3D array)

    The bold part is giving me an error:

    Java Code:
    import java.util.ArrayList;
    
    
    public class SudokuBoard {
    	
    	private ArrayList<Integer>[][] initialGrid, grid;
    	private final int ROW = 9, COLUMN = 9, EMPTY = 0;
    	
    	public SudokuBoard() {
    		initialGrid = [B]new ArrayList<Integer>[ROW][COLUMN][/B];
    	}
    The error states: Cannot create a generic array of ArrayList<Integer>

    Why not?

    Thanks in advance.
    Last edited by Lil_Aziz1; 05-15-2010 at 08:02 PM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,568
    Blog Entries
    7
    Rep Power
    21

    Default

    Arrays are way too stupid for generic types; i.e. they can't distinguish between an ArrayList<Double>[] and an ArrayList<Integer>[]. Partly that's because generic types are annihilated during runtime, i.e. both of the aforementioned arrays are treated as a raw type ArrayList[]. Arrays and generic types don't go well together.

    kind regards,

    Jos

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    So I can use an ArrayList but I'll have to explicitly cast (Integer) everytime right?

    EDIT: I removed the generic in the constructor, but I kept it when I initialized (forgot the word. Is it Instantiate? Anyhow, I'm talking bout the second line of code: private ArrayList<Integer>...) it. I get this message: Type safety: The expression of type ArrayList[][] needs unchecked conversion to conform to ArrayList<Integer>[][]

    Should I leave it like that?
    Last edited by Lil_Aziz1; 05-15-2010 at 08:06 PM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,568
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    So I can use an ArrayList but I'll have to explicitly cast (Integer) everytime right?
    You could do this:

    Java Code:
    ArrayList<Integer>[] a= new ArrayList[10];
    		
    a[0]= new ArrayList<Integer>();
    ... note the lack of a generic type when the array is constructed and also pay attention to the compiler warning.

    kind regards,

    Jos

Similar Threads

  1. arraylist to integer array
    By monika in forum Advanced Java
    Replies: 3
    Last Post: 04-26-2010, 09:09 PM
  2. Array and arraylist
    By MIA6 in forum New To Java
    Replies: 3
    Last Post: 11-29-2009, 02:22 AM
  3. Copying ArrayList into an Array
    By Manfizy in forum New To Java
    Replies: 6
    Last Post: 07-16-2009, 07:03 AM
  4. [SOLVED] Should I use an arraylist or an array?
    By matzahboy in forum New To Java
    Replies: 5
    Last Post: 11-17-2008, 04:34 AM
  5. Array to ArrayList
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-12-2007, 12:46 AM

Posting Permissions

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