Home java turn over array

turn over array

Author

Date

Category

why not an inverted array is 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 & gt; = 0; i -) {
      Arraysort [4 - i] = Massive [i];
      For (int a = 0; A & LT; 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);
    }
  }
}

Answer 1, Authority 100%

Why two arrays, why nested cycles?

Public Void Sort (int [] Massive) {
  For (int i = 0; i & lt; massive.length / 2; i ++) {
    INT TMP = Massive [i];
    Massive [i] = Massive [Massive.Length - I - 1];
    Massive [Massive.Length - i - 1] = TMP;
  }
}

Answer 2, Authority 80%

collections.reverse (array.aslist (source). ToArray (New int [Source.length]);

Yes, it will use redundant resources; No, it’s not scary

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


Answer 3

for (int i = 0; i & lt; array.length; i ++)
  Array [i] = (- 1) * Array [i];
Array.sort (array);
For (int i = 0; i & lt; array.length; i ++)
  Array [i] = (- 1) * Array [i];

Answer 4

Thank you all for the help, I figured out

Public Class Mane
{
  Public Void Sort (int [] Massive) {
    int [] arraysort = new int [5];
    for (int i = 4; i & gt; = 0; i -) {
      Arraysort [4 - i] = Massive [i];
    }
    For (int a = 0; A & LT; 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);
    }
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions