I wanted to implement the following Python function in Java. The function takes as a parameter an array of elements of type int and returns a new array, with the same elements as the original, but ordered in descending order, as in the Python function shown below
Function in Python:
def desc_sorted(lista):
new_array = [0 for i in range(len(lista))]
count = 0
for e in lista:
pos = 0
while (pos < count) and (e < new_array[pos]):
pos += 1
for pos2 in range(count, pos, -1):
new_array[pos2] = new_array[pos2 - 1]
new_array[pos] = e
count += 1
return new_array
Method in Java
import java.util.Arrays;
public class Ejercicio {
public static int[] arraySorted(int[] array) {
int[] new_array = new int[0 for (int i:array.length)]; // Error al definir el array
int count = 0;
for (int e:array) {
int posicionamiento = 0;
while (posicionamiento < count) && (e < new_array[posicionamiento]) { // Error en el operador && (and)
posicionamiento += 1;
for (int pos2:(colocados < posicionamiento - 1)) {
new_array[pos2] = new_array[pos2 - 1];
new_array[posicionamiento] = e;
count += 1;
}
return new_array;
}
}
}
public static void main (String[] args) {
int[] valores = {3, 8, 14, 33, 77, 2};
System.out.println(Arrays.toString(arraySorted(valores)));
}
}
This would be the Java "translation" of your Python method: