给List排序并且记录位置
程序员文章站
2022-06-09 16:30:11
...
//给List排序并且记录位置
{
int[] b = {0,0,2,1,1,2};
int[] bcopy = new int[b.length];
for (int i = 0; i < b.length; i++) {
bcopy[i] = b[i];
}
Arrays.sort(bcopy);
System.out.println("排序之前的序列为:" + Arrays.toString(b));
System.out.println("排序之后的序列为:" + Arrays.toString(bcopy));
List<Integer> locationList = new ArrayList<>();
for (int i = 0; i <b.length; i++) {
for (int j = 0; j <bcopy.length; j++) {
if (bcopy[i] == b[j] ){
System.out.println("排序之后的序列中 "+ bcopy[i] +" 的索引为:" +j);
locationList.add(j);
b[j] = 999999; //取一个在原序列中取不到的值
}
}
}
System.out.println(locationList);
}
上一篇: Python小白垃圾回收机制入门