L
Lenny Wintfeld
Here's a model of my problem. I see what's causing it (thinking in C++,
with pass by reference) but I don't see a Java way to solve it. Probably
because I'm so new to Java.
Here's the problem.
2 methods inside the same class. Method1 passes a String array to
method2, relying on method2 to process the string array and return a
String and a new String array for further processing back to method1.
public boolean method1 (String s1, other args)
{
//do some stuff
String [] sa1 = null; // to be created and returned by method2
String s2 = null; // to be created and returned by method2
Method2(s1, sa1, s2)l
//do some important stuff with sa1 and s2
return true;
}
// C++ type method (unfortunately)
private void method2 (String s1, String [] sa1, String s2)
{
//do some stuff
StringTokenizer st = new StringTokenizer(s1, "/");
String sRemainder;
sRemainder = st.nextToken();
s2 = st.nextToken(); // just a string to be returned
sa1 = sRemainder.split(","); // a string array to be returned
}
The eclipse debugger confirms that method 2 is processing the strings
exactly as I want it to. sa1 and s2 after the processing look great
inside method 2. But they're null after the return to method1. I assume
this is because sa1 and s2 are copies inside method 2 and they go out of
scope on the return. sa2 and s1 are created inside method2 but I need
them for further work in method 1.
I obviously coded this like a C/C++ programmer. What I'd like to know is
what's the conventional methodology (or programming idiom) for passing
back lots of stuff created in one method to it's caller? Feel free to
hack up the above example to show me how, if it's convenient.
Thanks in advance for your help/
Lenny Wintfeld
with pass by reference) but I don't see a Java way to solve it. Probably
because I'm so new to Java.
Here's the problem.
2 methods inside the same class. Method1 passes a String array to
method2, relying on method2 to process the string array and return a
String and a new String array for further processing back to method1.
public boolean method1 (String s1, other args)
{
//do some stuff
String [] sa1 = null; // to be created and returned by method2
String s2 = null; // to be created and returned by method2
Method2(s1, sa1, s2)l
//do some important stuff with sa1 and s2
return true;
}
// C++ type method (unfortunately)
private void method2 (String s1, String [] sa1, String s2)
{
//do some stuff
StringTokenizer st = new StringTokenizer(s1, "/");
String sRemainder;
sRemainder = st.nextToken();
s2 = st.nextToken(); // just a string to be returned
sa1 = sRemainder.split(","); // a string array to be returned
}
The eclipse debugger confirms that method 2 is processing the strings
exactly as I want it to. sa1 and s2 after the processing look great
inside method 2. But they're null after the return to method1. I assume
this is because sa1 and s2 are copies inside method 2 and they go out of
scope on the return. sa2 and s1 are created inside method2 but I need
them for further work in method 1.
I obviously coded this like a C/C++ programmer. What I'd like to know is
what's the conventional methodology (or programming idiom) for passing
back lots of stuff created in one method to it's caller? Feel free to
hack up the above example to show me how, if it's convenient.
Thanks in advance for your help/
Lenny Wintfeld