Results 1 to 6 of 6
  1. #1
    mimichi89 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Question Does a 2D Arraylist exist?

    I have created 2 regular 1-D Arrays but I want to stick them all into 1 ArrayList. Is this even possible. I want to tidy up the whole thing if possible. I am new to Java and this is for a homework assignment. My three arrays are all Integer Arrays so the ArrayList of Arrays would be of Integers if that makes any sense to anyone. Any help would be greatly appreciated!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: Does a 2D Arraylist exist?

    How about something like this?

    Java Code:
    List<List<Integer>> array2D = new ArrayList<List<Integer>>();
    ...
    // populate the list of lists
    ...
    for (List<Integer> list : array2D) {
          for (Integer i : list) {
             System.out.println(i);
          }
    }
    
    
    // to get individual elements, you would need to make multiple get calls.
    
    int val = array2D.get(4).get(3);  // could be row 4, col 3 or row 3, col 4 depending on how you construct the list(s).
    Last edited by jim829; 02-23-2013 at 10:45 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Does a 2D Arraylist exist?

    Moved from a staff-only section.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    mimichi89 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Re: Does a 2D Arraylist exist?

    Thank you jim829! It toke me a little to wrap myself around it but It works!

    By any chance do you know if it's possible to use an ArrayList of Integers as the parameter in a Random generator?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: Does a 2D Arraylist exist?

    I am not certain I know what you are asking. You can certainly pass a List or even List of Lists as an argument to a method which populates the list with random numbers.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default Re: Does a 2D Arraylist exist?

    I often use a "HashGrid" class that I made, that is essentially a HashMap<Point, T> with a few extra gets and puts for convenience. Doesn't necessarily work the same way as a 2d array, but I found it quite useful for a number of my projects.

Similar Threads

  1. package does not exist
    By loja11 in forum New To Java
    Replies: 2
    Last Post: 09-25-2011, 04:26 PM
  2. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  3. Exist or not exist file problem
    By Ballister in forum New To Java
    Replies: 6
    Last Post: 12-26-2010, 07:29 PM
  4. HttpClientLibrary does not exist
    By gbose in forum Networking
    Replies: 1
    Last Post: 02-03-2010, 07:21 PM
  5. package mj does not exist
    By PzK in forum New To Java
    Replies: 8
    Last Post: 12-03-2008, 10:30 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
  •