What are good implementation for swap() method in Java(pass by reference)?
-
void swap(int a,int b) I try to implement this method in Java,never works,how to pass by reference in Java
-
Answer:
The easiest way to pass by reference is to put the parameters in an array, and pass the array. So your method would look something like: void swap(int[] ab) { int temp = ab[0]; ab[0] = ab[1]; ab[1] = temp; } You can't do this with a method signature of swap(int a, int b) because by definition, that's pass-by-value.
Rich Freedman at Quora Visit the source
Related Q & A:
- Why cannot we use static keyword inside a method in java?Best solution by Stack Overflow
- How to get actual return type of a generic static method in Java?Best solution by stackoverflow.com
- How to Get Main output into Common() method in Java?Best solution by pages.cs.wisc.edu
- What is an Implementation Analyst?Best solution by Yahoo! Answers
- What do I need to swap a B18C1 into my civic?Best solution by honda-tech.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.