Why my code isnt working (20 lines) help please | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why my code isnt working (20 lines) help please

https://code.sololearn.com/cB8UGlgWH9Vt/?ref=app

20th Jan 2020, 4:11 PM
Денис Лапшин
Денис  Лапшин - avatar
4 Answers
+ 4
Your arrays are only swaped locally inside the function.
20th Jan 2020, 5:06 PM
Mihai Apostol
Mihai Apostol - avatar
+ 3
import java.util.Arrays; public class Test { static int[] one, two; //added variables for return public static void main(String[] args) { int[] one = {1, 2, 3}; // some two local arrays int[] two = {4, 5, 6}; swap(one, two); System.out.println(Arrays.toString(Test.one)); // print returned object System.out.println(Arrays.toString(Test.two)); // added Test. } public static void swap(int[] array1, int[] array2) { // one = array2; two = array1; int[] temp = array1; array1 = array2; array2 = temp; one = array1; // added return object with two variables two = array2; } }
20th Jan 2020, 6:23 PM
zemiak
0
thanks both, I've done this way https://code.sololearn.com/cSBH8IkG14bQ/?ref=app
20th Jan 2020, 7:16 PM
Денис Лапшин
Денис  Лапшин - avatar
0
ok, good for small arrays, but array two can't be shorter than array one
20th Jan 2020, 8:13 PM
zemiak