I am making a 2D array class in Java with wrap around edges. So in an 10×20 array, entry (2,-1) would be the same as (2,19) and (4,22) would be the same as (4,2). I also want to be able to access this structure as a “flattened out array” so I will be using a 1D array-like structure to store the objects. I would use a 1D array, but then I can’t use generic types, so instead I am using an ArrayList. The class isn’t done yet, but I posted the code below.
My first question is algorithmic: In the code below I access elements (i,j) using a modRow(i) and modCol(j) function. It works fine, but I’m wondering if anyone can think of a better way of doing this. These functions are then called by the get(i,j) function. I want to be able to access the elements as fast as possible.
My second question is specific to Java: I am fairly new to Java and am bringing this code over from a project in C++. I have a set() function that allows me to set an element at position i in the array (stored in the ArrayList). However, the set function will only work if the add() function was already called and set the element at position i in the flattened out array. If anyone has any suggestions as to how I could make this class work better I’d be happy to see them.
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);
}
}
Not sure what led you too believe you cannot use generics with arrays, because you can. So change your internal storage definition and call set to your hearts content.
Your modRows/modCols functions can be (slightly) more efficient. An index will be out of bounds in one direction only so you can get rid of your nested if checks.