On Sep 5, 12:07 pm, Norman Ho <[EMAIL PROTECTED]> wrote:
> I know this works:
>
> public static void main(String[] args) {
>
>         String[] arr = {"aaa","bbb"};
>         int i = 0, j = 1;
>         System.out.println("Before swap "
>                 + "arr[0] = " + arr[i]
>                 + " arr[1] = " + arr[j]);
>         swap(arr, i, j);
>         System.out.println("After swap "
>                 + "arr[0] = " + arr[i]
>                 + " arr[1] = " + arr[j]);
>     }
>
>     static void swap(String[] arr, int i, int j){
>         String temp = arr[i];
>         arr[i] = arr[j];
>         arr[j] = temp;
>     }
>
> But I don't understand why, they are both void.
Because the array is known before sending it to the static method,
therefore the swap works. In the method, you just change the internal
values of the array, a bit as if you were using an int declared before
the method, and changing it in the method; well, that's just an
analogy.
>
> If I go to the trouble of creating a class object with instance
> variables, and a method swapString, would it have worked?
Provided that you construct an array of those strings before the call
to the swapString method, yes. Otherwise, static or not static, you
will have the same trouble.

The fact is you cannot return more than one thing from a method, so if
you want to return two things, you have to create a collection of
them, and work with that collection.


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to