I'm working on this project using Arrays. I have a method called createRandomIntArray that creates an array. This method is meant to return the Array in descending order. I have been able to do just that but I want to know if there is a more effective way to write this method than the way I wrote it. I have my code below.
public static int[] createRandomIntArray(int n) {
Random random = new Random();
int[] result = new int[n];
for (int i = 0; i < n; i++) {
result[i] = random.nextInt(n);
}
Arrays.sort(result);
for (int i = 0; i < result.length / 2; i++) {
int temp = result[i];
result[i] = result[result.length - i - 1];
result[result.length - i - 1] = temp;
}
return result;
}
Aucun commentaire:
Enregistrer un commentaire