Results 1 to 13 of 13
  1. #1
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default Is it possible to create a 2D arraylist?

    New here, moderate exposure to Java (not advanced, but not exactly a beginner either).

    I've worked with 2D arrays before, but I didn't know if it was possible to create a 2D ArrayList. If so, how would it be defined?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I would think you can create a multidimensional array of any type.
    <type>[][] name = new <type>[3][]; // create 2 dim array of 3 elements

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

  4. #4
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    I would think you can create a multidimensional array of any type.
    <type>[][] name = new <type>[3][]; // create 2 dim array of 3 elements
    That's the thing, I can see the [3][], but I can't wrap my head around how to do the <type>. How would you get it to handle two dimensions? I'd think you'd have to have two <type> definitions, but how would you get them to work together.

  5. #5
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Try it and see what happens. Please report back on what happens.
    I have been trying, but just can't seem to figure out how to get it to work.

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Since this seems a specific enough question, I'll just provide the answer. Creating a 2d arraylist is indeed possible, just not in the way you're used to with arrays. You create an ArrayList, then you add ArrayLists to it, like this:
    Java Code:
    ArrayList<ArrayList<Integer>> toparr = new ArrayList<ArrayList<Integer>>();
    for(int i = 0; i < 10; i++)
      toparr.add(new ArrayList<Integer>());
    Accessing the elemenst is also a little different than arrays:
    Java Code:
    int a = toparr.get(2).get(1);
    This would fetch the ArrayList from toparr with the index 2, and then fetch the Integer from the contained ArrayList at index 1.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  7. #7
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Hmm, not what I expected. What I have been doing is using an array as the data in an arraylist.

    Object[] arrData = { 12345, "test" }

    ArrayList<Object> arrList = new ArrayList<Object>();
    toparr.add(arrData);


    May not be the exact code, but close.

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    But, do want a 2D ArrayList or do you want an ArrayList of arrays?

  9. #9
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    I'm happy using what I have. This was more a question of curiosity. I couldn't find it addressed anywhere on java.sun, or in any other sites or books that address ArrayList. I tried a couple ways to do it on my own, with no success, so I thought I would ask some "experts" to see if it was even a possibility.

    The "inner" array is a fixed-size array in my application (7 entries), but the "outer" array needs to be able to grow (hence the ArrayList). So the array-inside-an-ArrayList works fine.

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Then what you should be using is
    Java Code:
    Object[] arrData = { 12345, "test" }
    
    ArrayList<Object[]> arrList = new ArrayList<Object[]>();
    toparr.add(arrData);
    to, at least, make it a specific as possible.

    But, since what you're working with is a "mixed" array, it would probably make more sense to write a class instead of using an array. I.E. if your "fixed" array is really something that contains, say, an integer, two string, a double, a date, and two more strings then what you should is instead of
    Java Code:
    Obect[] a = new Object() { 0, "a", "b", 0.0, new Date(), "c", "d" };
    ... a[0] ... a[1] ...
    you should be writing a class
    Java Code:
    class Member {
      int id;
      String firstName;
      String lastName;
      double fee;
      Date joinDate;
      String preferredGroup;
      String alias;
    
      Member(int id, String firstName, String lastName, double ...) {
        this.id = id;
        this.firstName = firstName;
        ...
      }
    
      public int getId() { return id; }
    
      ...
    }
    
    Member m = new Member(0, "a", "b", 0.0, new Date(), "c", "d");
    ... m.getId() ... m.getFirstName() ...
    
    // and use ArrayList<Member>

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    how to do the <type>
    I meant the <type> to be meta. It should be replaced with any valid java type. For example:
    int[][] a2DimInt = new int[2][];
    ArrayList[][] a2DimAL = new ArrayList[2][];
    String[][] a2DimStr = new String[2][];
    etc

  12. #12
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Hmm, interesting. I'll give that some thought. Thanks.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Maybe a bit of Goedelization can be of any help: Goedelization is the mapping of more than one number to just one number. If no more than 2D matrixes are need the following mapping will do: given i and j the Goedelization could be (i << 16) | j for i and j < 2^16. A Map can come in handy too:

    Java Code:
    Map<Integer, Double> map= new HashMap<Integer, Double>
    ... and the following methods complete this trick:

    Java Code:
    public Double get(int i, int j) {
       return map.get((i << 16) | j);
    }
    
    public void set(int i, int j, double d) {
       map.put((i << 16) | j, d);
    }
    When the get( ... ) method returns null there was no element stored at (i, j).

    kind regards,

    Jos

Similar Threads

  1. Trying to create an undo function using an arraylist.
    By Spirit356 in forum New To Java
    Replies: 6
    Last Post: 04-27-2010, 07:01 PM
  2. Help with ArrayList
    By nura23 in forum New To Java
    Replies: 4
    Last Post: 01-10-2010, 01:23 PM
  3. More on ArrayList
    By JavaJ in forum New To Java
    Replies: 0
    Last Post: 12-04-2009, 03:20 PM
  4. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  5. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 08:48 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
  •