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... ;)

.



Relevant Pages

  • Re: How to get a raw IP-addr into byte-array?
    ... and sign-extends it to an int, which it passes to the println method. ... I assume that they mean an array like: ... my Java book clearly states that a byte is a SIGNED ...
    (comp.lang.java)
  • Collision detection
    ... Java Tutorial. ... Part of my part-time university course has been to write a Java Applet ... int xMouse, yMouse; ... public void mousePressed{ ...
    (comp.lang.java.programmer)
  • Re: 2 GIG the new 640 MB
    ... >>program will ever be able to create an array with more than 4 billion ... > autocasting it back to int when required. ... Java doesn't even see pointers -- unless one looks carefully. ...
    (comp.lang.java.advocacy)
  • Re: Revolving TextArea???
    ... I have only been doing Java for about a month. ... public int getDocumentLength() { ... public void insertString(int offs, String str, AttributeSet a) ...
    (comp.lang.java.programmer)
  • Best way to create an array of Strings in a called method
    ... Thus, if I want a method to return an array of Strings, then I must do ... public void setStringArray{ ... Given what I understand of references (and that Java doesn't use ...
    (comp.lang.java.programmer)