[英]2D array class in java with wrap around edges
我正在用Java环绕边缘制作2D数组类。 因此,在10x20数组中,条目(2,-1)与(2,19)相同,而(4,22)与(4,2)相同。 我还希望能够以“扁平化数组”的形式访问此结构,因此我将使用类似于1D数组的结构来存储对象。 我将使用一维数组,但随后无法使用泛型类型,因此我使用的是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);
}
}
不知道是什么导致您也相信不能将泛型与数组一起使用,因为可以。 因此,更改您的内部存储定义并将呼叫集更改为您的满意内容。
您的modRows / modCols函数可以(稍微)更有效。 索引只会在一个方向上超出范围,因此如果检查,您可以摆脱嵌套。
if(index < 0) return index + max_size;
else if(index >= max_size) return index % max_size;
else return index;
@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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.