Results 1 to 5 of 5
  1. #1
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    6

    Default Generics Q: array of lists of arrays

    SSSCE - there are two questions in the comments.

    Java Code:
    import java.util.LinkedList;
    import java.util.List;
    
    public class Foo {
    	
    	public static void main(String[] args) {
    		
    		// i want to create an array of lists of arrays.  this seems like the
    		// right syntax, but gives an error: "Cannot create a generic array of
    		// LinkedList<Foo[]>".  what is the correct syntax?
    		// List<Foo[]>[] arrayOfLists = new LinkedList<Foo[]>[10];
    
    		// this works, but complains about type safety. can I do anything
    		// about this warning other than @SuppressWarnings("unchecked")?
    		List<Foo[]>[] arrayOfLists = new LinkedList[1];
    		
    		// the declaration is actually already typesafe, because this line
    		// produces a type mismatch error:
    		// arrayOfLists[0] = new LinkedList<String>();
    		
    		// this works
    		arrayOfLists[0] = new LinkedList<Foo[]>();
    	}
    
    }
    Get in the habit of using standard Java naming conventions!

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    6

    Default Re: Generics Q: array of lists of arrays

    Anyone? Anyone? Bueller?
    Get in the habit of using standard Java naming conventions!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Generics Q: array of lists of arrays

    Look up type erasure as I think that this is causing your problem. It's a basic limitation of Java generics. :(

    This may help clarify: http://www.angelikalanger.com/Generi...rs.html#FAQ202

    You may be better off creating a List of Lists:

    Java Code:
            List<List<Foo>> listOfLists = new LinkedList<List<Foo>>();
            listOfLists.add(new ArrayList<Foo>());
    Last edited by Fubarable; 01-22-2012 at 05:45 PM.

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    6

    Default Re: Generics Q: array of lists of arrays

    Yeah... or for my particular purpose, a Map<Integer, List<Foo>>.

    Thanks for that link.
    Get in the habit of using standard Java naming conventions!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Generics Q: array of lists of arrays

    Yep, that would work too. You're welcome and good luck!

    I just wish that generics weren't added as an after-thought.

Similar Threads

  1. array lists in jsp and google api
    By gedas in forum New To Java
    Replies: 3
    Last Post: 04-07-2011, 02:18 PM
  2. generics and passing arrays
    By TopNFalvors in forum New To Java
    Replies: 0
    Last Post: 03-17-2011, 04:19 PM
  3. Array Lists help!!
    By lilika in forum New To Java
    Replies: 12
    Last Post: 01-04-2011, 02:05 PM
  4. Convert arrays into lists
    By Richard2732 in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 08:53 AM
  5. Help with arrays and array lists
    By ambernicole88 in forum New To Java
    Replies: 3
    Last Post: 12-04-2009, 09:47 PM

Posting Permissions

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