我有一个整数数组,我想要一些代码为我生成一个具有这些规范的数组:
假设
int totalvalue[] = {100, 120, 90, 110}
我想要这个
{1,3,0,2}
答案 0 :(得分:0)
你可以这样做: -
public static void main(String[] args) {
Integer totalvalue[] = { 100, 120, 90, 110 };
List<Integer> original = Arrays.asList(totalvalue);
List<Integer> copy = new ArrayList<Integer>(original);
Collections.sort(copy, new Comparator<Integer>() {
@Override
public int compare(Integer intger1, Integer intger2) {
return intger2.compareTo(intger1);
}
});
int[] finalArray = new int[totalvalue.length];
for (int i = 0; i < totalvalue.length; i++) {
finalArray[i] = original.indexOf(copy.get(i));
}
System.out.println(Arrays.toString(finalArray));
}
答案 1 :(得分:-1)
我会借用最近从这里删除的别人代码;-)
public static void main(String[] args){
int array[] = { 90, 100, 90, 110 };
Map<Integer, Integer> valueIndexMap = new HashMap<Integer, Integer>();
int index = 0;
for (int entry : array)
valueIndexMap.put(entry, index++);
//perform your sorting algorithm here on valueIndexMap.key
Integer[] newArray = new Integer[array.length];
for (int i = 0; i < array.length; i++) {
newArray[i] = valueIndexMap.get(array[i]);
}
System.out.println(Arrays.asList(newArray));
}
这里有一些https://www.cs.cmu.edu/~adamchik/15-121/lectures/Sorting%20Algorithms/sorting.html
并且研究它们很重要,因为它们是CS的基础
祝你好运