Re: method parameters
david wrote:
Yeah, I just did some research about this issue and find the same as
you suggested.
This is my second program with the JAVA, I need to use it to write
several programs that would be cross-platform. And at the same time I
reading one small book, it will take several weeks before I will be
able to understand the idea of the JAVA better. But I it looks that I
capable of writing JAVA code, just maybe it's abit too much objective,
that means you need to know more about the standard classes.
The basic idiom for emulating pass by reference is to wrap the value in
an array.
public void someMethod( int w[] )
{
w[0] = 42;
}
will let you set v to 42 when called like this:
int v = 0;
someMethod( new int[] {v} );
But it's very awkward because you already have an array. (It's even
pretty awkward when you don't have an array.) A better method might be
to do as Lew suggests and just wrap your array in a class
public class BoolMatrix {
private boolean m [][];
public void resetSize( int x, int y ) {
m = new boolean[x][y];
}
}
Now things will work much more like you'd expect.
BoolMatrix n = new BoolMatrix();
int size = 42;
someOtherMethod( n, size );
// ...
public void someOtherMethod( BoolMatrix x, int size )
{
x.resetSize( size, size );
}
This is much clearer to read and debug, I think. Few surprises here, at
least until you get into multiple threads... ;)