Java 对具有重复项的数组排序后获取唯一索引

Java 对具有重复项的数组排序后获取唯一索引,java,sorting,indexing,Java,Sorting,Indexing,我需要对数组进行排序,并获取链接到未排序数组的索引。问题是,如果未排序的数组包含重复项,即[1,1,1,2,2,],则这些项的索引相同。例如[3,5,5,3,3,]索引将是[0,1,1,0,0]。但是我需要获得以下索引[0,3,4,1,2]。如何做到这一点 ArrayList<Double> nfit = new ArrayList<Double>(); ArrayList<Double> nfit_copy = new ArrayList<Double

我需要对数组进行排序,并获取链接到未排序数组的索引。问题是,如果未排序的数组包含重复项,即
[1,1,1,2,2,]
,则这些项的索引相同。例如
[3,5,5,3,3,]
索引将是
[0,1,1,0,0]
。但是我需要获得以下索引
[0,3,4,1,2]
。如何做到这一点

ArrayList<Double> nfit = new ArrayList<Double>();
ArrayList<Double> nfit_copy = new ArrayList<Double>(nfit);

// Fill nfit

Collections.sort(nfit);

int[] ind = new int[nfit.size()];

for (int n = 0; n < nfit.size(); n++){
    ind[n] = nfit_copy.indexOf(nfit.get(n));
}
ArrayList nfit=new ArrayList();
ArrayList nfit_copy=新的ArrayList(nfit);
//填充nfit
集合。排序(nfit);
int[]ind=新的int[nfit.size()];
对于(int n=0;n这是因为您使用了nfit\u copy.indexOf(nfit.get(n))如果您有重复的编号,它将为您提供相同的索引

比如说

[3,5,5,3,3,]
--->每次使用时:
nfit.indexOf(3)
将为您提供索引
0

因此,您可能需要更改此值或将其设置为null(不要重新设置它,因为它会更改索引),以便获得下一个重复的数字

试试这个:

ArrayList<Integer> nfit = new ArrayList<Integer>();
ArrayList<Integer> nfit_copy = new ArrayList<Integer>(nfit);

// Fill nfit 

  nfit.add(3);
  nfit.add(5);
  nfit.add(5);
  nfit.add(3);
  nfit.add(3);


  nfit_copy = (ArrayList<Integer>) nfit.clone();


  Collections.sort(nfit);

  int[] ind = new int[nfit.size()];

  for (int n = 0; n < nfit.size(); n++) {
         ind[n] = nfit_copy.indexOf(nfit.get(n));
         nfit_copy.set(nfit_copy.indexOf(nfit.get(n)), null);
   }
   for (int i = 0; i < ind.length; i++) {
         int j = ind[i];
         System.out.println(j);
    }
ArrayList nfit=new ArrayList();
ArrayList nfit_copy=新的ArrayList(nfit);
//填充nfit
增加(3);
增加(5);
增加(5);
增加(3);
增加(3);
nfit_copy=(ArrayList)nfit.clone();
集合。排序(nfit);
int[]ind=新的int[nfit.size()];
对于(int n=0;n
公共静态void main(字符串[]args){
整数[]输入=新整数[]{1,2,2,1,5,6,2,3,2,3,4,5,6,1};
List MappeIndex=new ArrayList();
列表排序=新建ArrayList();
for(int num:input){
已排序。添加(num);
}       
集合。排序(已排序);
用于(整数编号:输入){
System.out.println(“查找输入->”+编号);
对于(int i=0;i”+i);
地图索引。添加(i);
打破
}
}
}
}
System.out.println(MappedIndex);
}

这是我用
Set
HashMap
结构的解决方案。请阅读代码注释以了解更多信息

int[] input = { 45, 3, 4, 9, 2, 1, 45, 3 };
// Backup the initial array for later use.
int[] originalArray = Arrays.copyOf(input, input.length);    
Arrays.sort(input);

// Determine all occurences with a set.
Set<Integer> set = new HashSet<Integer>();
for (int i : input) {
    set.add(i);
}

// Populate a hashmap for keeping <value,index> pairs.
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int counter = 0;
for (int i : set) {
    map.put(i, counter++);
}

// Populate the output array.
int[] output = new int[input.length];
for (int i = 0; i < output.length; i++) {
    output[i] = map.get(originalArray[i]);
}
int[]input={45,3,4,9,2,1,45,3};
//备份初始阵列以供以后使用。
int[]originalArray=Arrays.copyOf(input,input.length);
数组。排序(输入);
//使用集合确定所有发生的事件。
Set=newhashset();
for(int i:输入){
增加(i);
}
//填充哈希映射以保留对。
HashMap=newHashMap();
int计数器=0;
for(int i:set){
map.put(i,counter++);
}
//填充输出数组。
int[]输出=新的int[input.length];
for(int i=0;i

就这些。如果您将
输出的内容打印到控制台,您可以看到结果。

它为nfit提供了不正确的结果。添加(3);增加(3);增加(5);增加(3);增加(3);。结果是0 1 3 4 2,而不是0 1 4 2 3。如何
0 1 4 2 3
?索引中有数字3(0,1,3,4),索引
2
中有一个
5
,请再次检查:)可能我误解了你的想法。我检查了Tee-Gee的代码(见下文),它为数组[3,3,5,3,3]提供了0 1 4 2 3,这对我来说似乎是正确的。
0 1 4 2 3
将意味着您的数组在排序后是
3 3 5 3
,因此这不是您想要的排序索引,对我来说似乎不正确:)我们讨论了不同的事情。在本例中,您将索引分配给未排序的数组(很抱歉,这正是我所需要的),但Tee-Gee提供了排序索引。
int[] input = { 45, 3, 4, 9, 2, 1, 45, 3 };
// Backup the initial array for later use.
int[] originalArray = Arrays.copyOf(input, input.length);    
Arrays.sort(input);

// Determine all occurences with a set.
Set<Integer> set = new HashSet<Integer>();
for (int i : input) {
    set.add(i);
}

// Populate a hashmap for keeping <value,index> pairs.
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int counter = 0;
for (int i : set) {
    map.put(i, counter++);
}

// Populate the output array.
int[] output = new int[input.length];
for (int i = 0; i < output.length; i++) {
    output[i] = map.get(originalArray[i]);
}