views:

366

answers:

4

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 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;
/* ... */
+4  A: 

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.

James Black
I think this is the simplest solution to the problem at hand. Thanks.(Other readers may want to check out Stephen C's solution, below)
Alex Wilson
+2  A: 

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.

Stephen C
A bit much work to give special handling to one function, however, it definitely seems like the best general solution. I'll keep this sort of thing in mind for the future. Much appreciated.
Alex Wilson
+1  A: 

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.

Omry
Sorry for the ambiguity. It was of type int[][].
Alex Wilson
A: 

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".

meriton