signed

QiShunwang

“诚信为本、客户至上”

给List排序并且记录位置

2021/6/3 17:14:58   来源:
//给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);
        }