我在Java中创建一个带有环绕边缘的2D数组类。因此,在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中)。但是,set函数仅在已调用add()函数并将该元素设置在flattened out数组中的位置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 :(得分:1)
不确定是什么导致你也认为你不能将泛型用于数组,因为你可以。因此,请将内部存储定义和调用集更改为您的内容。
您的modRows / modCols函数可以(稍微)更高效。索引只能在一个方向上超出范围,因此您可以摆脱嵌套的if检查。
if(index < 0) return index + max_size;
else if(index >= max_size) return index % max_size;
else return index;
答案 1 :(得分: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;