ArrayList的已排序元素的初始索引

时间:2013-04-03 14:20:45

标签: java sorting collections arraylist

以下代码按升序实现nfit的排序。

public static void main(String[] args) {
    ArrayList<Double> nfit = new ArrayList<Double>();

    nfit.add(2.0);
    nfit.add(5.0);
    nfit.add(1.0);
    nfit.add(8.0);
    nfit.add(3.0);

    // Sort individuals in ascending order
    Collections.sort(nfit);

    System.out.print(nfit);

}

输出结果为:

[1.0, 2.0, 3.0, 5.0, 8.0]

我的问题是如何获取已排序元素的初始索引?在这个例子中,我的问题的答案如下:

[2, 0, 4, 1, 3]

我如何获得这些索引?

6 个答案:

答案 0 :(得分:4)

复制ArrayList并排序,然后使用indexOf。

ArrayList<Double> nfit = new ArrayList<Double>();
nfit.add(2.0);
nfit.add(5.0);
nfit.add(1.0);
nfit.add(8.0);
nfit.add(3.0);
ArrayList<Double> nstore = new ArrayList<Double>(nfit); // may need to be new ArrayList(nfit)
Collections.sort(nfit);
int[] indexes = new int[nfit.size()];
for (int n = 0; n < nfit.size(); n++){
    indexes[n] = nstore.indexOf(nfit.get(n));
}
System.out.println(Arrays.toString(indexes));

如果您想要ArrayList中的索引,

Collections.sort(nstore);
for (int n = 0; n < nfit.size(); nfit++){
    nstore.add(n, nfit.indexOf(nstore.remove(n)));
}
Collections.sort(nfit);

这将导致一个排序的ArrayList nfit和一个索引nstore的ArrayList。

答案 1 :(得分:1)

制作ArrayList的副本,然后对副本进行排序。之后使用indexOf方法传递已排序数组的元素并调用原始ArrayList

答案 2 :(得分:1)

如果值是唯一的,您可以使用Map<Double, Integer>来保存值和初始订单。您只需要对地图键进行排序并获得每个键的对应值。

答案 3 :(得分:0)

两种解决方案:1)如果您与ArrayList结合,则需要使用Iterator来构建解决方案集的索引列表。 2)切换到包含值和顺序的地图(可以通过排序更改)。

答案 4 :(得分:0)

以下mergesort实现实现了一个方法&#39; indexSort&#39;这样就可以避免搞乱输入数组。 http://algs4.cs.princeton.edu/22mergesort/Merge.java.html http://algs4.cs.princeton.edu/code/javadoc/Merge.html#indexSort(java.lang.Comparable[])

答案 5 :(得分:0)

冒泡排序永远是你的朋友。您可以使用它对数组进行排序,同时跟踪原始数组索引。当你的数组中有多个类似的元素时,它也会起作用。

import java.util.*;
import java.util.Arrays;
import java.util.Random;
public class BubbleSort {
    public static void main(String[] args) {
        Random rnd = new Random();
        int count = 5;
        ArrayList<Double> array = new ArrayList<Double>();
        int[] indices = new int[count];
        for(int i = 0; i < indices.length; i++) {
            array.add(rnd.nextDouble() * count);
            indices[i] = i;
        }
        ArrayList<Double> values = new ArrayList<Double>(array);
        bubbleSort(values, indices);
        System.out.println("Unsorted input array   :" + array);
        System.out.print("Sorted   input array   : ");
        for(int i = 0; i < array.size(); i++) {
            System.out.print(array.get(indices[i]) + " " );
        }
        System.out.println();
    }
    private static void bubbleSort(ArrayList<Double> values, int[] indices) {
        for(int k = 0; k < values.size(); k++) {
            for(int l = k + 1; l < values.size(); l++) {
                if(values.get(k) > values.get(l)) {
                    double temp_value = values.get(k);
                    values.set(k, values.get(l));
                    values.set(l, temp_value);
                    int temp_index = indices[k];
                    indices[k] = indices[l];
                    indices[l] = temp_index;
                }
            }
        }
    }
}

我多年后回答这个问题的原因是accepted answer如果您有多个具有相似值的元素会失败,因为在indexes[n] = nstore.indexOf(nfit.get(n));中,indexOf方法始终返回第一个指定元素的出现导致输入数组中具有类似元素的索引数组不正确。

最后,复制并粘贴BubbleSort.java中的代码,然后运行javac BubbleSort.java && java BubbleSort进行编译并查看结果。