Results 1 to 9 of 9
  1. #1
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default array of ArrayLists

    Eclipse is telling me it is impossible to make an array of ArrayLists. Is this indeed true? And if so, what would be the best workaround?

    Im making a program storing data in several (16) ArrayLists, using them as queues. I add an element to the list, check it's size, if it's larger then some number determined elsewhere, I remove the first number.

    I could just do every action, instead of an iteration over an array, seperatly in code, but that would be extremely ugly. I could write some kind of wrapper class, also ugly and very weird if thats necesary (why prohibit making arrays of ArrayLists then?). A final solution could be to change the working of my program...

  2. #2
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    I created one using the jdk compiler and it just works fine.

    Could you provide the code and tell us what Eclipse says about it?

    Erik
    I'm new to Java but I like to help where ever I can. :)

  3. #3
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Java Code:
    public class Main {
    	public static void main(String[] args)
    	{
    		ArrayList<Double>[] prijzen=[U]new ArrayList<Double>[16];[/U]        
        }
        
    }
    Gives the error:
    "Cannot create a generic array of ArrayList<Double>"
    With a red line under the underlined part.

  4. #4
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Ah, that doesn't work on my machine either. Being quite new to java this is beyond my knowledge. You could create a ArrayList of ArrayList<Double> though.
    I'm new to Java but I like to help where ever I can. :)

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

  6. #6
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    @Fubarable: that's interesting. Could you point me to some more information on this?
    I'm new to Java but I like to help where ever I can. :)

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by venerik View Post
    @Fubarable: that's interesting. Could you point me to some more information on this?
    It's called "Type Erasure", and you'll want to read that very section here: Wikipedia: Java Generics

    It's been done for backwards compatibility but some feel that it does hinder the usefulness of generics some. C# for instance does not have this problem.

  8. #8
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Thanks for the reference.
    I'm new to Java but I like to help where ever I can. :)

  9. #9
    Severe is offline Member
    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    Default Re: array of ArrayLists

    You could also try the following:

    Java Code:
    ArrayList<ArrayList<String>> colsPerRow = new ArrayList<ArrayList<String>>();

Similar Threads

  1. ArrayLists
    By Freakzoyd in forum New To Java
    Replies: 4
    Last Post: 11-12-2010, 05:27 AM
  2. ArrayLists for BlueJ
    By heyit'skaye in forum New To Java
    Replies: 1
    Last Post: 09-01-2010, 05:15 AM
  3. ArrayList of ArrayLists
    By coolnfunky_raj in forum New To Java
    Replies: 10
    Last Post: 07-03-2008, 11:07 AM
  4. A Map implemented with ArrayLists
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:29 PM
  5. arraylists problem
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 08:38 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
  •