int[] reverseArray = new int[20];
int index = 0;
// reverse the array
for (int i = array.length - 1; i >= 0; i--)
{
reverseArray[index] = array[i];
// System.out.println(array);
index++;
}
array = Arrays.copyOf(reverseArray, 20);
for (int i = 0; i < array.length; i++)
{
System.out.println(array[i]); //<-------- this prints out in descending order
}
I sorted the array and then reversed it. Since you need the original array I then copied it back into the variable array. Printing out the array shows that it is in descending order but it is not transferring over to the main method. Is it out of scope? I don't know why because when printed in main it is still in ascending order??? I tried to change the return value of the sort method to int[] but apparently that is cheating....
Thankspackage com.codegym.task.task08.task0826;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
/*
Five winners
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int[] array = new int[20];
for (int i = 0; i < array.length; i++) {
array[i] = Integer.parseInt(reader.readLine());
}
// int[] newArray = new int[20];
sort(array);
System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);
System.out.println(array[3]);
System.out.println(array[4]);
}
public static void sort(int[] array) {
//write your code here
// sort the array
Arrays.sort(array);
// initialize new array
int[] reverseArray = new int[20];
int index = 0;
// reverse the array
for (int i = array.length - 1; i >= 0; i--)
{
reverseArray[index] = array[i];
// System.out.println(array);
index++;
}
array = Arrays.copyOf(reverseArray, 20);
for (int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}
}
}