【问题标题】:2D array class in java with wrap around edgesjava中带有环绕边缘的二维数组类
【发布时间】:2012-01-30 00:49:24
【问题描述】:

我正在用 Java 制作一个带有环绕边缘的二维数组类。因此,在 10x20 数组中,条目 (2,-1) 将与 (2,19) 相同,并且 (4,22) 将与 (4,2) 相同。我还希望能够以“扁平数组”的形式访问此结构,因此我将使用类似一维数组的结构来存储对象。我会使用一维数组,但是我不能使用泛型类型,所以我使用的是 ArrayList。课程还没有完成,但我在下面发布了代码。

我的第一个问题是算法问题:在下面的代码中,我使用 modRow(i) 和 modCol(j) 函数访问元素 (i,j)。它工作正常,但我想知道是否有人能想到更好的方法。然后这些函数由 get(i,j) 函数调用。我希望能够尽快访问元素。

我的第二个问题是针对 Java 的:我对 Java 还很陌生,并且正在从 C++ 项目中引入这段代码。我有一个 set() 函数,它允许我在数组中的位置 i 处设置一个元素(存储在 ArrayList 中)。但是,只有当 add() 函数已被调用并将元素设置在展平数组中的位置 i 时,set 函数才会起作用。如果有人对我如何让这门课更好地工作有任何建议,我会很高兴看到他们。

import java.util.*;

public class Grid<T> {

    ArrayList<T> array; // holds objects in grid - would rather use an array
                        // but that won't support generics
    int rows;  // number of rows in grid
    int cols;  // number of cols in grid
    int length; // total number of objects in grid - maybe just get this from ArrayList
    int conNum; // number of connections for each point on grid (either 4 or 8) 
                // used for determining distances between points on the grid

    // constructor
    Grid(int row, int col, int numCon) {
        rows = row;
        cols = col;
        length = rows * cols;
        conNum = numCon;
        array = new ArrayList<T>(length);
    }

    // returns total size of grid
    public int len() {
        return length;
    }

    // returns number of rows
    public int row() {
        return rows;
    }

    // returns number of columns
    public int col() {
        return cols;
    }

    // wish I didn't have to use this 
    // would be nice to just be able to use set(int i, T t)
    public void add(T t) {
        array.add(t);
    }

    // sets object i in flattened out array
    public void set(int i, T t) {
        array.set(i, t);
    }

    // returns object i in flattened out array
    // for faster access when user just needs to iterate through all objects 
    // in grid without respect to position in 2D grid
    public T get(int i) {
        return array.get(i);
    }

    // returns the row position of i in grid - adjusted for warp around edges 
    // is there a better way to do this?  
    private int modRow(int i) {
        if(i >=0) {
            if(i < rows) {
                return i;
            } else { // i >= rows
                return i % rows;
            }
        } else { // i < 0
            return (i % rows + rows) % rows;
        }
    }

    // returns the column position of j in grid - adjusted for warp around edges 
    // is there a better way to do this? 
    private int modCol(int j) {
        if(j >=0) {
            if(j < cols) {
                return j;
            } else { // j >= cols
                return j % cols;
            }
        } else { // j < 0
            return (j % cols + cols) % cols;
        }
    }

    // sets object at (i,j) value from store adjusted for wrap around edges 
    public void set(int i, int j, T t) {
        array.set(modRow(i) * cols + modCol(j), t);
    }

    // gets object at (i,j) value from store adjusted for wrap around edges 
    public T get(int i, int j) {
        return array.get(modRow(i) * cols + modCol(j));
    }

    // returns distance on the grid between two objects at (y1,x1) and (y2,x2)
    public int dist(int y1, int x1, int y2, int x2) {
        int y = distFirst(y1, y2);
        int x = distSecond(x1, x2);
        if (conNum == 4) // taxicab distance
        {
            return y + x;
        } else { //if(conNum == 8) supremum distance
            return Math.max(y, x);
        }
    }

    // returns distance on the grid between the first coordinates y1 & y2 of two objects
    public int distFirst(int y1, int y2) {
        int dist = Math.abs(modRow(y2) - modRow(y1));
        return Math.min(dist, rows - dist);
    }

    // returns distance on the grid between the second coordinates x1 & x2 of two objects
    public int distSecond(int x1, int x2) {
        int dist = Math.abs(modCol(x2) - modCol(x1));
        return Math.min(dist, cols - dist);
    }
}

【问题讨论】:

    标签: java arrays arraylist


    【解决方案1】:

    不确定是什么导致您也相信您不能将泛型与数组一起使用,因为您可以。因此,更改您的内部存储定义并将调用集设置为您的心脏内容。

    您的 modRows/modCols 函数可以(稍微)更有效率。索引只会在一个方向上越界,因此您可以摆脱嵌套的 if 检查。

    if(index < 0) return index + max_size;
    else if(index >= max_size) return index % max_size;
    else return index;
    

    【讨论】:

    • 感谢您的代码。对于 index = -max_size 时才有效。虽然我不打算低于它,但它可能对我来说已经足够了。
    • 我尝试了 T[] array,然后 array = new T[length],但这给了我一个错误。
    【解决方案2】:

    @Perception : 原来的index &lt; 0 校验有什么问题?

    if(index < 0) return (index % max_size + max_size) % max_size;    // <-- like that
    else if(index >= max_size) return index % max_size;
    else return index;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-15
      • 2018-02-05
      • 1970-01-01
      相关资源
      最近更新 更多