8

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.

4

4 回答 4

7

听起来您想使用行键、列键,然后是该位置的值。没有内置的数据结构可以为您做到这一点。

最容易使用的可能是实际数据的二维数组。使用类似下面的内容从行名或列名转到数组中的实际索引。根据需要添加任意数量的名称到索引绑定。

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

然后在网格中获取该值...

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

get(String rowName, String colName)如果您想要更简洁的 API,请将网格和方法放在一个类中。

编辑:我看到问题已更新,看起来名称-索引对对于行和列都是相同的。所以这是一个更新的版本:

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)];
    }
}
于 2009-03-26T04:37:26.377 回答
1

(根据评论编辑)

如果大小是在运行时确定的,那不是问题。这可能有效:

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;
于 2009-03-26T03:55:55.850 回答
0

数组可以在运行时调整大小。如果您的行/列大小不会经常变化,并且数据不是太稀疏,那么数组是您最好的选择。

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;
    }
}
于 2009-03-26T03:55:44.680 回答
0

你可以只使用像

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) {
  //
}
于 2009-06-28T11:33:56.787 回答