简体   繁体   English

什么数据结构最适合在Java中实现二维数组?

[英]What data structure is most suitable for implementing a 2-D array in Java?

I want to implement a 2-D array kind of a thing. 我想实现一个2-D数组的东西。

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; 我不想使用数组,因为2-D数组需要在程序的早期声明,但它不是固定的; 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. 我也希望遍历这个二维数据结构,就像我通过Map一样。

It sounds like you want to use a row-key, a col-key, and then the value at that location. 听起来你想要使用行键,col键,然后使用该位置的值。 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. 如果需要更清晰的API get(String rowName, String colName)请将网格和get(String rowName, String colName)方法放在类中。

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)];
    }
}

(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;

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) {
  //
}

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM