class Solution{
static List<Integer> get(int a,int b)
{
a=a+b;
b=a-b;
a=a+b;
//**What will be the return statement?**
}
}
Asked
Active
Viewed 59 times
-1

Turing85
- 18,217
- 7
- 33
- 58

Abhijeet Tripathi
- 21
- 2
-
1Since [Java is pass-by-value always](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value), we would need to return an object of some kind, holding the swapped values for `a` and `b`. – Turing85 Apr 11 '22 at 18:43
-
2Your method should return a `List` according to your signature. – PM 77-1 Apr 11 '22 at 18:44
-
1You don't need the three lines you've already written in that method. You just need to return the list, with the values swapped. You could consider using the `Arrays.asList` method, for example. – Dawood ibn Kareem Apr 11 '22 at 18:51
1 Answers
1
Your question/doubt is not that clear but I'm guessing that what you need is something like this:
The super easy fast solution:
public List<Integer> swapInput(int a, int b) {
return Arrays.asList(b, a);
}
The long (not that necessary) solution:
public List<Integer> swapInput(int a, int b) {
System.out.println("----- Before swap -----");
System.out.println("First: " + a);
System.out.println("Second: " + b);
//First parameter is assigned to a temporary var
int temporary = a;
//Second parameter will be now assigned as first
a = b;
//The temporary parameter (which contains the initial value of the first parameter) is assigned as second
b = temporary;
System.out.println("----- After swa -----");
System.out.println("First: " + a);
System.out.println("Second: " + b);
return Arrays.asList(a, b);
}

ypdev19
- 173
- 2
- 13