2012-01-30 34 views
0

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

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

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

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

回答

0

不知道是什么导致你也相信你不能在数组中使用泛型,因为你可以。所以,改变你的内部存储定义和呼叫集到你的心中。

您的modRows/modCols函数可以(稍微)更高效。一个索引只会在一个方向上出界,所以你可以摆脱嵌套的if检查。

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

感谢您的代码。对于索引<0,这只有在索引> = -max_size时才有效。虽然我不打算低于这个水平,但这对我的使用可能足够了。 – COM 2012-01-30 01:55:37

+0

我尝试了T []数组,然后array = new T [length],但是这给了我一个错误。 – COM 2012-01-30 01:57:31

0

@Perception:原始index < 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;