Results 1 to 7 of 7

Thread: Multidimensional ArrayList in java

  1. #1
    Join Date
    Dec 2009
    Posts
    213

    Multidimensional ArrayList in java

    Hello,
    I wanted to know if it was possible to establish a multidimensional ArrayList in java? For example, a table tab [][][] but using an ArrayList instead of a table? And how do you access an element using multiple indexes? Can I overload the methOfA get (index) by causing get (index, index)? Or is there a there another way? Thank you in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    330

    Re: Multidimensional ArrayList in java

    Hello,
    I would say that this is not defined by default but that it defines very quickly since the coordinates [x] [y] are equivalent to x_max * y + x. So yes it can overload and this new method would only add more. The [] [] in C + + is an abstraction of [] it can logically understand. I think these are the advanced topics in java, I guess you are clear with the java core concept. If not, then i would recommend you to clear the java core first.

  3. #3
    Join Date
    Nov 2009
    Posts
    583

    Re: Multidimensional ArrayList in java

    Hi,
    Not if you use the ArrayList to simulate multidimensional arrays, you can not just rely on a formula style x_max * y + x (indeed even with tables that is not true): array [x] [y] is not at all equivalent to array [y * x_max + x]. Regarding the ArrayList of ArrayList (simulating arrays), the method get (index, index) would:
    Code:
    Object get(int x int y) {
      Object row = this.get(x);
      return row instanceof ArrayList
         ? ((ArrayList) row).get(y);
         : null;
    }
    I am assuming this instance of a class derived from ArrayList.

  4. #4
    Join Date
    Nov 2009
    Posts
    330

    Re: Multidimensional ArrayList in java

    Hey,
    That's why I mentioned C + + but I ought to make the first line is true. My method derive from C + + and gives only tables "rectangular". It depends actually use that one wants to do. Your method is a solution, I own and can do 2D "non-rectangular. It is (as in Java) On the fact that the table size is a 2D array of Objects that are themselves arrays. In short my solution is good for a table (fixed size) but not ideal for a scalable collection (ArrayList). But the issue was a collection so I'll go to sleep.

  5. #5
    Join Date
    Nov 2009
    Posts
    359

    Re: Multidimensional ArrayList in java

    Hello,
    I do not want to contradict you, but in Java an array of arrays is an array of objects, since a table is an object. So even in the case of rectangular tables, the formula
    Code:
    tab[x][y] = Tab[y + x * x_max]
    is false (indeed it is rather Y_MAX * x + y). You just can not do transposition of index calculation multi-dimensional -> mono-dimensional.

  6. #6
    Join Date
    Nov 2009
    Posts
    343

    Re: Multidimensional ArrayList in java

    Hello,
    I think you can also do the same thing by the following line.
    Code:
    ArrayList <ArrayList <ArrayList <String>>> list = new ArrayList <ArrayList <ArrayList <String>>>();
    And access to the element i, j, k is as follows without reimplementing the get method:
    Code:
    list.get(i).get(j).get(k);
    I hope this will hep you. If you have any more problem then do post back.

  7. #7
    Join Date
    Feb 2011
    Posts
    1

    Re: Multidimensional ArrayList in java

    Quote Originally Posted by Windowed View Post
    Hello,
    I think you can also do the same thing by the following line.
    Code:
    ArrayList <ArrayList <ArrayList <String>>> list = new ArrayList <ArrayList <ArrayList <String>>>();
    And access to the element i, j, k is as follows without reimplementing the get method:
    Code:
    list.get(i).get(j).get(k);
    I hope this will hep you. If you have any more problem then do post back.
    Hi,

    I don't if I am following this. I am however interesting in creating a data structure using ArrayList but the data will be made up a collection of elements with their corrsponding x,y coordinates. Confusing for because the set method takes only one number as index. Can you please be kind and help me so promtly as I have a work to submit. Thanks! BR, Kisimi

Similar Threads

  1. Multidimensional ArrayList in Java
    By Navp in forum Software Development
    Replies: 1
    Last Post: 05-02-2011, 12:43 AM
  2. ArrayList Collection problem in java
    By Bricklayer in forum Software Development
    Replies: 4
    Last Post: 23-07-2010, 01:18 PM
  3. How to sort items of an ArrayList in java?
    By hounds in forum Software Development
    Replies: 4
    Last Post: 06-02-2010, 07:46 PM
  4. Problem in modifying value of ArrayList Object of JAVA
    By Praetor in forum Software Development
    Replies: 3
    Last Post: 16-11-2009, 09:58 AM
  5. How to implement MultiDimensional Array in JAVA
    By Nihar Khan in forum Software Development
    Replies: 3
    Last Post: 28-02-2009, 01:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,274,795.76167 seconds with 17 queries