我想用 Java 实现以下 Python 函数。该函数将 int 类型的元素数组作为参数,并返回一个新数组,其元素与原始数组相同,但按降序排列,如下所示的 Python 函数
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
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)));
}
}
这将是您的 Python 方法的 Java“翻译”: