Why is not the inverted array displayed? that is, the last element of the array should be the first, and so on.

public class Mane { public void sort(int[] massive){ int[]arraySort = new int[10]; for(int i = 4; i >= 0; i--){ arraySort[4 - i] = massive[i]; for(int a = 0; a < 5; a++){ massive[a] = arraySort[a]; } } } public static void main(String[] arg){ int[] mass = {1,2,3,4,5}; Mane m = new Mane(); m.sort(mass); for(int i: mass){ System.out.println(i); } } } 
  • Have you tried debug? - Vartlok
  • I'm certainly not an expert, but even the code is too messy. - Andrew Bystrov

4 answers 4

Why two arrays, why nested loops?

 public void sort(int[] massive) { for (int i = 0; i < massive.length / 2; i++) { int tmp = massive[i]; massive[i] = massive[massive.length - i - 1]; massive[massive.length - i - 1] = tmp; } } 
  • I do not know why, but I have java.lang.ArithmeticException: divide by zero - Andrey Shpileva
  • @AndreyShpileva where did you find the division by 0? - Raider
  • not me, but the studio found - Andrey Shpileva
  • Ask her then. - Raider
 Collections.reverse(Arrays.asList(source)).toArray(new int[source.length]); 

yes, it will use redundant resources; no it's not scary

https://stackoverflow.com/a/22828623/2908793

  • one
    Justify the phrase "no, it's not scary." Are you by any chance a Google Chrome developer? - Raider
  • Cool, but it won't work on primitives, java.util.Collections.reverse returns nothing, toArray(new int[whatever]) will not work either. ZIS FROM JAVA! 11 - zRrr
  • 2
    In addition, Arrays.asList creates a view above the array, and the set calls on this list change the source array, i.e. for Integer[] source = {1, 2, 3, 4, 5}; calling Collections.reverse(Arrays.asList(source)) will rearrange the source . - zRrr
  • @zRrr yes, I fell for it , the answer was written in parallel while talking in the office, shame on me. - etki
  • one
    The end user doesn’t care how much time is spent on the code - the main thing is for it to work and work quickly. Your code: 1) does not work, 2) even if corrected, it creates an unnecessary copy of the array in memory. If you teach a beginner, then why just bad? - Raider

Thank you all for your help, I figured it out

 public class Mane { public void sort(int[] massive){ int[]arraySort = new int[5]; for(int i = 4; i >= 0; i--){ arraySort[4 - i] = massive[i]; } for(int a = 0; a < 5; a++){ massive[a] = arraySort[a]; } } public static void main(String[] arg){ int[] mass = {1,2,3,4,5}; Mane m = new Mane(); m.sort(mass); for(int i: mass){ System.out.println(i); } } } 
  • And you watched the answers at all? Just wondering. - Raider
  • watched if interested - Sergey
  • What is not arranged? - Raider
  • everyone liked it, but for some reason it didn’t work as it should - Sergey
  • Use my one-cycle option. It does not create a copy of the array, and does not depend on its length. - Raider
 for(int i = 0; i < array.length; i++) array[i] =(-1)*array[i]; Arrays.sort(array); for(int i = 0; i < array.length; i++) array[i] =(-1)*array[i]; 
New member
Andrey Kremenets is a new member of the site. Be lenient in asking clarifying questions, commenting and answering. Read about the norms of behavior .