java中具有环绕边的2D数组类
我正在用Java制作一个带有环绕边的2D数组类。因此,在10x20数组中,条目(2,-1)与(2,19)相同,(4,22)与(4,2)相同。我还希望能够以“展平阵列”的形式访问此结构,因此我将使用类似一维阵列的结构来存储对象。我会使用1D数组,但是我不能使用泛型类型,所以我使用的是ArrayList。该类尚未完成,但我在下面发布了代码 我的第一个问题是算法:在下面的代码中,我使用modRow(I)和modCol(j)函数访问元素(I,j)。它工作得很好,但我想知道是否有人能想出更好的方法来做这件事。这些函数随后由get(i,j)函数调用。我希望能够尽可能快地访问元素java中具有环绕边的2D数组类,java,arrays,arraylist,Java,Arrays,Arraylist,我正在用Java制作一个带有环绕边的2D数组类。因此,在10x20数组中,条目(2,-1)与(2,19)相同,(4,22)与(4,2)相同。我还希望能够以“展平阵列”的形式访问此结构,因此我将使用类似一维阵列的结构来存储对象。我会使用1D数组,但是我不能使用泛型类型,所以我使用的是ArrayList。该类尚未完成,但我在下面发布了代码 我的第一个问题是算法:在下面的代码中,我使用modRow(I)和modCol(j)函数访问元素(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);
}
}
import java.util.*;
公共类网格{
ArrayList array;//在网格中保存对象-宁愿使用数组
//但这不支持泛型
int rows;//网格中的行数
int cols;//网格中的列数
int length;//网格中的对象总数-可能只是从ArrayList获取
int conNum;//网格上每个点的连接数(4或8)
//用于确定栅格上点之间的距离
//建造师
网格(int行、int列、int numCon){
行=行;
cols=col;
长度=行*列;
conNum=numCon;
数组=新的ArrayList(长度);
}
//返回网格的总大小
公共int len(){
返回长度;
}
//返回行数
公共int行(){
返回行;
}
//返回列数
公共int col(){
返回cols;
}
//但愿我不用用这个
//只要能使用set(inti,tt)就好了
公共无效添加(T){
数组。添加(t);
}
//在展平阵列中设置对象i
公共无效集(int i,T){
数组.set(i,t);
}
//返回展平数组中的对象i
//当用户只需要遍历所有对象时,可以更快地访问
//在栅格中,不考虑二维栅格中的位置
公共不获取(int i){
返回数组get(i);
}
//返回网格中i的行位置-已针对边的扭曲进行调整
//有更好的方法吗?
私有内部modRow(内部i){
如果(i>=0){
如果(i<行){
返回i;
}else{//i>=行
返回i%行;
}
}else{//i<0
返回(i%行+行)%行;
}
}
//返回网格中j的列位置-已针对边的扭曲进行调整
//有更好的方法吗?
私有内部模式(内部j){
如果(j>=0){
if(j=cols
返回j%cols;
}
}else{//j<0
返回(j%cols+cols)%cols;
}
}
//从为环绕边调整的存储将对象设置为(i,j)值
公共空集(int i,int j,T){
数组.set(modRow(i)*cols+modCol(j),t);
}
//从为环绕边调整的存储中获取(i,j)值处的对象
公共T-get(int i,int j){
返回数组.get(modRow(i)*cols+modCol(j));
}
//返回(y1,x1)和(y2,x2)处两个对象在栅格上的距离
公共内部区(内部y1、内部x1、内部y2、内部x2){
int y=距离优先(y1,y2);
int x=距离秒(x1,x2);
if(conNum==4)//出租车距离
{
返回y+x;
}else{//if(conNum==8)上确界距离
返回Math.max(y,x);
}
}
//返回网格上两个对象的第一个坐标y1和y2之间的距离
公共int distFirst(int y1、int y2){
int dist=Math.abs(modRow(y2)-modRow(y1));
返回Math.min(距离,行-距离);
}
//返回网格上两个对象的第二个坐标x1和x2之间的距离
公共整数秒(整数x1,整数x2){
int dist=Math.abs(modCol(x2)-modCol(x1));
返回Math.min(dist,cols-dist);
}
}
不确定是什么导致您也相信不能将泛型用于数组,因为您可以。因此,请将内部存储定义和调用集更改为您的内容
您的modRows/modCols函数可以(稍微)更高效。索引只能在一个方向上越界,这样您就可以摆脱嵌套的if检查
if(index < 0) return index + max_size;
else if(index >= max_size) return index % max_size;
else return index;
if(索引<0)返回索引+最大大小;
否则,如果(索引>=max\u size)返回索引%max\u size;
else回归指数;
不确定是什么导致您也相信不能将泛型用于数组,因为您可以。因此,请将内部存储定义和调用集更改为您的内容
您的modRows/modCols函数可以(稍微)更高效。索引只能在一个方向上越界,这样您就可以摆脱嵌套的if检查
if(index < 0) return index + max_size;
else if(index >= max_size) return index % max_size;
else return index;
if(索引<0)返回索引+最大大小;
否则,如果(索引>=max\u size)返回索引%max\u size;
else回归指数;
@Perception:原始索引<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;
if(索引<0)返回(索引%max\u size+max\u size)%max\u size;//=最大大小)返回索引%max\u size;
else回归指数;
@Perception:原版的有什么问题吗