Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 05-02-2010
Member
 
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.
Reply With Quote
  #2  
Old 05-02-2010
Member
 
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.
Reply With Quote
  #3  
Old 05-02-2010
Member
 
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.
Reply With Quote
  #4  
Old 05-02-2010
Member
 
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.
Reply With Quote
  #5  
Old 05-02-2010
Member
 
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.
Reply With Quote
  #6  
Old 05-02-2010
Member
 
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.
Reply With Quote
  #7  
Old 16-02-2011
Member
 
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 04:17 PM.