Re: method parameters
- From: Mark Space <markspace@xxxxxxxxxxxxxx>
- Date: Sun, 11 May 2008 13:38:47 -0700
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... ;)
.
- References:
- method parameters
- From: david
- Re: method parameters
- From: Eric Sosman
- Re: method parameters
- From: david
- Re: method parameters
- From: david
- method parameters
- Prev by Date: Re: passing a Factory to a method to create a generic instance
- Next by Date: Re: tomcat with java 1.5 -1.6
- Previous by thread: Re: method parameters
- Index(es):
Relevant Pages
|