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 < 20; i++) {
array[i] = Integer.parseInt(reader.readLine());
}
sort(array);
for (int x : array) {
System.out.println(x);
}
}
public static void sort(int[] array) {
ArrayList<Integer> number = new ArrayList<Integer>();
for (int x : array)
number.add(x);
Collections.sort(number);
Collections.reverse(number);
for (int i = 0; i < number.size(); i++)
array[i] = number.get(i);
}
}
My code works and conditions satisfied , but why we do not need return statement ?
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Gellert Varga
17 August 2021, 18:03
I have slightly rewritten the program you shared.
Well, I didn't rewrite it a bit!
I only changed the names of a few variables for better understanding:
The variables AddressOfArray and passedAddressOfArray: both store the same address. They point to the same object.
When you work with the passedAddressOfArray variable (line 25), you are making changes to the same array object that the AddressOfArray variable points to (printing loop on line 11.)
Therefore, it is not necessary to return anything. 0
Guadalupe Gagnon
17 August 2021, 13:23
The method is void:
public static void sort(int[] array)
This means it does not return anything and therefore does not require a return statement.
0