Code

It turns out only 1,2,2,1; .. I know that the simplest task, I - the first course.

Closed due to the fact that off-topic participants are Roman C , Sergey Glazirin , aleksandr barakin , cheops , 0xdb Sep 30 '18 at 18:50 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Roman C, Sergey Glazirin, aleksandr barakin, cheops, 0xdb
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What are you trying to do? How exactly to "flip"? - entithat
  • Mirror)) I mean, 1,2,3,5 to 5,3,2,1 - Santa Murphy
  • one
  • one
    Tobish Collections.reverse () - GenCloud
  • 2
    @SantaMurphy, the code must be inserted into the question with text, not a screenshot. - Qwertiy

2 answers 2

The problem is that you go through the whole array.

1 2 3 4 i=0 i-----^ 4 2 3 1 i=1 i-^ 4 3 2 1 i=2 ^-i 4 2 3 1 i=3 ^-----i 1 2 3 4 

But I had to stop in the middle of the array:

 for (int i=0; i<a.length/2; ++i) меняем a[i] и a[a.length-1-i] местами 

or generally use two variables

 for (int i=0, j=a.length-1; i<j; ++i, --j) меняем a[i] и a[j] местами 

Personally, the second approach seems more beautiful to me.

PS: And in order to avoid such questions, there is a debugger that will show at every step what the values ​​of the variables are equal to.

  • 2
    fyudutper - sounds funny :) - entithat
  • one
    @entithat, yes, I switched the layout in vain))) - Qwertiy

for (int i = 0; i <array.length / 2; i ++) {...}

  • Yes, / 2 already added, but the answer is now 1.2 - Santa Murphy
  • Your code works fine, you probably check with the output in the reverse cycle of the operation, and not the end result - Alexander Verbitsky
  • public class MyClass {public static void main (String args []) {int [] array = new int [] {1,2,3,4,5}; for (int i = 0; i <array.length; i ++) {System.out.print (array [i] + ""); } System.out.println (); for (int i = 0; i <array.length / 2; i ++) {int tmp = array [i]; array [i] = array [array.length-1-i]; array [array.length-1-i] = tmp; } for (int i = 0; i <array.length; i ++) {System.out.print (array [i] + ""); }}} - Alexander Verbitsky