Results 1 to 13 of 13
- 06-15-2010, 02:34 AM #1
- 06-15-2010, 02:44 AM #2
I would think you can create a multidimensional array of any type.
<type>[][] name = new <type>[3][]; // create 2 dim array of 3 elements
-
Try it and see what happens. Please report back on what happens.
- 06-15-2010, 03:04 AM #4
- 06-15-2010, 03:04 AM #5
- 06-15-2010, 06:53 AM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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:
Accessing the elemenst is also a little different than arrays:Java Code:ArrayList<ArrayList<Integer>> toparr = new ArrayList<ArrayList<Integer>>(); for(int i = 0; i < 10; i++) toparr.add(new ArrayList<Integer>());
This would fetch the ArrayList from toparr with the index 2, and then fetch the Integer from the contained ArrayList at index 1.Java Code:int a = toparr.get(2).get(1);
Ever seen a dog chase its tail? Now that's an infinite loop.
- 06-15-2010, 12:06 PM #7
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.
- 06-15-2010, 12:20 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
But, do want a 2D ArrayList or do you want an ArrayList of arrays?
- 06-15-2010, 12:26 PM #9
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.
- 06-15-2010, 12:38 PM #10
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Then what you should be using is
to, at least, make it a specific as possible.Java Code:Object[] arrData = { 12345, "test" } ArrayList<Object[]> arrList = new ArrayList<Object[]>(); toparr.add(arrData);
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
you should be writing a classJava Code:Obect[] a = new Object() { 0, "a", "b", 0.0, new Date(), "c", "d" }; ... a[0] ... a[1] ...
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>
- 06-15-2010, 01:08 PM #11
I meant the <type> to be meta. It should be replaced with any valid java type. For example:how to do the <type>
int[][] a2DimInt = new int[2][];
ArrayList[][] a2DimAL = new ArrayList[2][];
String[][] a2DimStr = new String[2][];
etc
- 06-15-2010, 01:59 PM #12
Hmm, interesting. I'll give that some thought. Thanks.
- 06-15-2010, 05:49 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
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:
... and the following methods complete this trick:Java Code:Map<Integer, Double> map= new HashMap<Integer, Double>
When the get( ... ) method returns null there was no element stored at (i, j).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); }
kind regards,
Jos
Similar Threads
-
Trying to create an undo function using an arraylist.
By Spirit356 in forum New To JavaReplies: 6Last Post: 04-27-2010, 07:01 PM -
Help with ArrayList
By nura23 in forum New To JavaReplies: 4Last Post: 01-10-2010, 01:23 PM -
More on ArrayList
By JavaJ in forum New To JavaReplies: 0Last Post: 12-04-2009, 03:20 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
ArrayList
By kizilbas1 in forum New To JavaReplies: 1Last Post: 01-12-2008, 08:48 PM


LinkBack URL
About LinkBacks


Bookmarks