java - 可以检查对象字段的通用选择排序

标签 java sorting generics arraylist selection-sort

我需要做的是获取一个包含名称字段的文件,然后还包含数据,并根据代表一组特定数据的给定数字对它们进行排序。这是一个示例文本文件。这就像城市及其每个月的平均温度(实际上并不准确)。

NewYork: 35, 28, 99, 39, 3, 15, 52, 5, 6, 97, 36, 32
Baltimore: 1, 59, 55, 0, 92, 82, 23, 60, 23, 16, 75, 75
Seattle: 19, 18, 10, 36, 50, 2, 8, 36, 56, 86, 14, 91
Atlanta: 57, 75, 52, 66, 28, 58, 53, 5, 21, 30, 81, 58

我希望能够根据给定的数据点对它们进行排序,因此如果选择 2,则应按第二个点按升序排序,因此它看起来像这样。每个城市的实际数据并未排序,整个数据集根据正在使用的数据点向上或向下移动。

Seattle: 19, 18, 10, 36, 50, 2, 8, 36, 56, 86, 14, 91
NewYork: 35, 28, 99, 39, 3, 15, 52, 5, 6, 97, 36, 32
Baltimore: 1, 59, 55, 0, 92, 82, 23, 60, 23, 16, 75, 75
Atlanta: 57, 75, 52, 66, 28, 58, 53, 5, 21, 30, 81, 58
              ^
             This is the second data point row that was sorted.

这是选择排序类的样子,我可以让它在没有泛型的情况下工作,但我希望能够使用泛型。

public class SelectionSort{

private SelectionSort(){

}

public static <T extends Comparable<T>> void sort(ArrayList <T> arrayList, int key){

    for(int i=0; i<arrayList.size() -1; i++)
    {
        int smallestIndex = i;

        for(int j=i+1; j<arrayList.size(); j++)
        {
            if(arrayList.get(smallestIndex).compareTo((arrayList.get(j))) > 0  )
            {
                smallestIndex = j;
            }
        }
        T temp = arrayList.get(i);
        arrayList.add(i,arrayList.get(smallestIndex));
        arrayList.add(smallestIndex, temp);

    }

}

然后对于实际对象,我有这样的构造函数的输入只是文本文件中的一整行。然后数据句柄和名称句柄帮助拆分字符串并将名称设置为文本文件中的名称,并将数组列表设置为该名称所包含的所有数字。

public class ObjectData<T> {
private ArrayList<Integer> list;
private String name;



public ObjectData(String objectInfo){

    String array[] = allData(objectInfo);
    this.name = nameHandle(array[0]);
    list = new ArrayList<Integer>(Arrays.asList(dataHandle(array[1])));
}



private static String[] allData(String string){

    String array[] = string.split(":");


    return array;

}

private static String nameHandle(String string){
    String name = string.trim();

    return name;

}

private Integer[] dataHandle(String string){
    String array[] = string.split(",");
    String trimmedArray[] = new String[array.length];
    Integer integerArray[] = new Integer[trimmedArray.length];

    for (int i = 0; i < array.length; i++){
        trimmedArray[i] = array[i].trim();
    }

    for (int i = 0; i < trimmedArray.length; i++){
        integerArray[i] = Integer.valueOf(trimmedArray[i]);
    }


    return integerArray;
}



public String getName(){

    return this.name;

}


public Integer getIndex(int index){


    return list.get(index);

}

}

在 main 中,所有对象都放入一个数组列表中,然后将其输入到 SelectionSort 类中。我遇到的问题是弄清楚如何对这样的对象使用通用选择排序。例如,如果 key 是上面的 2,我打算只调用。我会打电话

if(arrayList.get(smallestIndex).getIndex(2).compareTo((arrayList.get(smallestIndex).getIndex(2))) > 0 )

而不是 if(arrayList.get(smallestIndex).compareTo((arrayList.get(j))) > 0 )

我考虑能够做到这一点的唯一其他方法是,我不创建对象,而是将所有信息放入二维数组中,然后能够比较它们,但随后我丢失了数据的名称。如果有人可以建议我应该如何改变它以使其发挥作用,或者以完全不同的方式来实现这一目标,那就太好了。

最佳答案

您可能考虑的一种方法是创建一个实现 Comparator 的对象接口(interface)并以要排序的索引作为参数:

public class DataComparator implements Comparator<ObjectData>{
    private int sortIndex;

    public DataComparator(int sortIndex){
        this.sortIndex = sortIndex;
    }

    public int compare(ObjectData t1, ObjectData t2) {
        return (t1.getIndex(sortIndex) < t2.getDataIndex(sortIndex)) ? -1 : 1;
    }
}

如果您有 List<ObjectData>您的城市,我们称之为 cityList例如,您可以使用 Collections.sort(cityList, new DataComparator(2)) 对其进行排序.

关于java - 可以检查对象字段的通用选择排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47662595/

相关文章:

java - 跟踪 lambda 的执行

java - 既不是 BindingResult 也不是 bean 的普通目标对象

java - MigLayout 中的 Glue 等效项

c# - 为什么要在 List<T>.AddRange(IEnumerable<T>) 中添加额外的副本?

c# - 泛型和显式/隐式运算符

java - Maven:压缩资源并保存到特定文件夹中

sorting - 转换类型进行排序 : any runtime cost?

c++ - 在 python/C++ 中快速生成用于排序矩阵的 key

java - 移动 2D ArrayList 中的元素

c++ - 如果基类没有成员,派生 C++ 类的大小是多少?