Creating a reference in Java
I want to use a reference as a shorthand to an array element, where the array consists of elements of a basic type (integers). How can I explicitly 开发者_StackOverflow社区create a reference in Java? Is it possible?
/* ... */
//neighbors = new int[][]
//I'm using neighbor, below, to refer to an element in neighbors so that the code
//is easier to read. I want it to be a reference so that I can write to it.
int neighbor = neighbors[y][x];
if (neighbor == 0)
btn.setText("");
else
btn.setText("" + neighbor);
if (btn.isEnabled() == true) {
numSquaresRevealed++;
btn.setEnabled(false);
}
//I want neighbor to be a reference so that I can be consistent and use it below
if (changeNeighbors)
neighbors[y][x] = -1;
/* ... */
What you could do is to change your logic so that you pass in neighbors[x][y] and write out the new value of what you passed in.
neighbors[x][y] = setButton(neighbors[x][y]);
private int setButton(int neighbor) {
btn.setText((neighbor == 0 ? "" : "" + neighbor));
if (btn.isEnabled() == true) {
numSquaresRevealed++;
btn.setEnabled(false);
}
//I want neighbor to be a reference so that I can be consistent and use it below
return (changeNeighbors ? -1 : neighbor);
}
This doesn't use a reference but it may do what you want, which is to make the application more readable.
The Java language and standard libraries won't help you. Instead, I think you'll need to implement it by hand. One approach is to create and use a mutable holder class; e.g.
public class IntHolder {
private int value;
public IntHolder(int value) { this.value = value; }
public int getValue() { return this.value; }
public void setValue(int value) { this.value = value; }
}
and then replace your int[][]
declarations, etc with IntHolder[][]
.
Another alternative would be to create your own array reference classes; e.g.
public class IntArray2DElement {
private int[][] array;
private int x;
private int y;
public IntArray2DElement( int[][] array, int x, int y) {
this.array = array; this.x = x; this.y = y; }
public int getValue() { return this.array[x][y]; }
public void setValue(int value) { this.array[x][y] = value; }
}
However, it would probably be simpler program around the problem; e.g. by remembering the x
and y
values explicitly. Or do as @James suggests.
it's not clear from your question if the type of your array is
Integer[][] neighbors;
or
int[][] neighbors;
the reason is that due to Java auto-unboxing, this compiles fine:
int n = neighbors[x][y];
and is in fact actually
int n = neighbors[x][y].intValue();
in any case, you can only have a reference to a non primitive type, so if your array is of type Integer, you can simply do:
Integer n = neighbors[x][y];
if your array types is int[][], your 'reference' will have to contain a reference to the array itself, and two indexes into it.
however, since Integer is immutable, you can't write to it even if you had a reference to it, so your only real option is to keep a reference to the array and two indexes.
Contrary to C++, Java only support references to objects, not to members of objects, and not to elements of arrays.
(Perhaps because it simplifies Garbage Collection.)
Therefore, you can either make neighbor a full object, or live with using the array with the indexes as "reference".
精彩评论