views:

556

answers:

5

I want to implement a 2-D array kind of a thing.

What data structure will be most suitable for this? An array or some other data-structure will do. If there is any other data structure which will satisfy my requirement, then please tell me.

I don't want to use an array because the 2-D array needs to be declared early in the program but it is not fixed; the size will be determined at run time.

Also, the number of rows will be equal to the number of columns; this is fixed, because the same name will be given to both the rows and the columns.

I also want to traverse through this 2-D data structure as I would through a Map.

A: 

Arrays can be sized at runtime. If you have a row/column size that doesn't vary too often, and the data is not too sparse, then an array is your best bet.

class TwoDimArray {
    public int[][] createArray(int nRows, int nCols) {
        return new int[nRows][nCols];
    }
    public int[][] resizeArray(int[][] oldArray, int nRows, int nCols) {
        int[][] newArray = new int[nRows][nCols];
        for (int i=0; i<Math.min(oldArray.length, nRows); ++i)
            for (int j=0; j<Math.min(oldArray[i].length, nCols); ++j)
                newArray[i][j] = oldArray[i][j];
        return newArray;
    }
}
Chris Dolan
Thanks for the program. It definately helped..
AGeek
Ok, i want to have it like this... I want that each row and column should have a name thru which it can mapped later on.. As there is a key in Map data structure in Java.util package... Is there any other way out...
AGeek
+1  A: 

(Edits based on comment)

If the size is determined at runtime that is not an issue. This might work:

final int[][]              data;
final int                  size;
final Map<String, Integer> names;

// code that sets the size variable
names = new HashMap<String, Integer>();
data  = new int[size][size];

names.put("ID-A", 0);
names.put("ID-B", 1);

data[names.get("ID-A")][names.get("ID-A")] = 39;
data[names.get("ID-A")][names.get("ID-B")] = 40;
data[names.get("ID-B")][names.get("ID-A")] = 41;
data[names.get("ID-B")][names.get("ID-B")] = 42;
TofuBeer
okk,, i vl try and use this only..
AGeek
Ok, i want to have it like this... I want that each row and column should have a name thru which it can mapped later on.. As there is a key in Map data structure in Java.util package... Is there any other way out...
AGeek
+4  A: 

It sounds like you want to use a row-key, a col-key, and then the value at that location. There's no builtin data structure that'll do that for you.

The easiest thing to use may be a 2d array for the actual data. Use something like the following to go from a row or column name to the actual index in your array. Add as many name-to-index bindings as you want.

Map<String, Integer> rows = new HashMap<String, Integer>();
Map<String, Integer> cols = new HashMap<String, Integer>();

Then getting that value in the grid...

grid[rows.get("Row name")][cols.get("Column name")];

Put the grid and a get(String rowName, String colName) method in a class if you want a cleaner API.

Edit: I see the question has been updated, and it looks like the name-to-index pairs are the same for both rows and columns. So here's an updated version:

class SquareMap<V> {
    private V[][] grid;
    private Map<String, Integer> indexes;

    public SquareMap(int size) {
        grid = (V[][]) new Object[size][size];
        indexes = new HashMap<String, Integer>();
    }

    public void setIndex(String name, int index) {
        indexes.put(name, index);
    }

    public void set(String row, String col, V value) {
        grid[indexes.get(row)][indexes.get(col)] = value;
    }
    public V get(String row, String col) {
        return grid[indexes.get(row)][indexes.get(col)];
    }
}
Nikhil Chelliah
The idea behind ur program looks more cleaner and efficient to use.. But i have not understo od the use of Grid..how do i use this grid in my program...
AGeek
Hi, Please could you please explain me the functioning of this Grid... What is this grid and how can i store value in it...
AGeek
A: 

It is very easy.

A: 

You could just use a Map like

class TwoDArray<V> implements Iterable<Map.Entry<Point, V>> {
  private final Map<Point, V> map = new LinkedHashMap<Point, V>();
  public V set(int x, int y, V value) {
     return map.put(new Point(x,y), value);
  }
  public V get(int x, int y) {
     return map.get(new Point(x, y));
  }
  public Iterator<Map.Entry<Point, V>> iterator() {
     return map.entrySet().iterator();
  }
}

// to iterate
TwoDArray<Double> twoDArray = new TwoDArray();
twoDArray.set(3, 5, 56.0);
twoDArray.set(-1000, 5, 123.4);
twoDArray.set(789012345, -100000000, -156.9);
for(Map.Entry<Point, Double> entry: twoDArray) {
  //
}
Peter Lawrey