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.
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.
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.
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.
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.
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.
Re: Multidimensional ArrayList in java
Quote:
Originally Posted by
Windowed
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