Results 1 to 2 of 2
  1. #1
    stevenlin598 is offline Member
    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Default 2D Array of ArrayList

    So currently I'm trying to create a 2D Array of ArrayLists. I know I can switch Array to ArrayList, but that's a bad fix.
    This part works
    Java Code:
    private ArrayList<Thing>[][] world;
    I just can't assign the variable correctly.
    Java Code:
    world = new ArrayList<Thing>[size][size];
    That throws something that says can't create a generic array of ArrayList<Thing>. Help?

  2. #2
    kalata is offline Member
    Join Date
    Aug 2011
    Location
    Bulgaria
    Posts
    29
    Rep Power
    0

    Default Re: 2D Array of ArrayList

    Well, go on without the generic part:
    Java Code:
    world = new ArrayList[size][size];
    Here we create the array object itself and arrays are not generic.
    When you initialize for example world[0][0] then the generics are on and world[0][0] = new ArrayList<Thing>(); will be ok.
    But world[0][0] = new ArrayList<Totally different thing>(); will lead to compilation error(type mismatch).

    Cheers,
    Kalin
    Last edited by kalata; 06-07-2013 at 01:13 AM.

Similar Threads

  1. Array or ArrayList?
    By rhym1n in forum New To Java
    Replies: 1
    Last Post: 03-08-2013, 02:08 PM
  2. Array vs ArrayList
    By Lund01 in forum New To Java
    Replies: 5
    Last Post: 10-14-2010, 11:41 AM
  3. 2D array of ArrayList (technically a 3D array)
    By Lil_Aziz1 in forum New To Java
    Replies: 3
    Last Post: 05-15-2010, 09:11 PM
  4. Array and arraylist
    By MIA6 in forum New To Java
    Replies: 3
    Last Post: 11-29-2009, 03:22 AM
  5. Array to ArrayList
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-12-2007, 01: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
  •