【问题标题】:Selection sort with arraylists?使用数组列表进行选择排序?
【发布时间】:2017-03-19 04:05:00
【问题描述】:

我必须为学校的事情调整选择排序。目的是返回玩家的排名,获胜者优先等。排名相同的玩家应该按照他们在锦标赛列表中出现的顺序列出。

public ArrayList<Player> ranking() {
    ArrayList<Player> result = new ArrayList<Player>();
    // Supply this code!
    return result;

现在这是我调整选择排序的尝试

    int smallInt = 0;
    int j=0;
    int smallIntIndex = 0;      

    for(int i=1;i<players.size();i++){
        smallInt = result.get(i-1).totalScore();
        smallIntIndex = i-1;
        for(j=i;j<players.size();j++){
            if(result.get(j).totalScore()<smallInt){
                smallInt = result.get(j).totalScore();
                smallIntIndex = j;                    
            }
        }
        int temp = result.get(smallIntIndex).totalScore();
        result.set(smallIntIndex, result.get(i-1));
        result.set(i-1, temp);
    }
    return result;

唯一让我出错的是最后一行

result.set(i-1, temp); //The method set(int, Player) in the type 
                       //ArrayList<Player> is not applicable for the arguments (int, int)

知道我做错了什么吗?大部分改编是正确的吗?任何帮助表示赞赏。谢谢

附言请不要建议比较器或类似的东西,这不是我要找的。​​p>

【问题讨论】:

  • 错误说明了一切。将变量 temp 更改为 Player 类型,而不是 int
  • 外循环必须从索引0开始,NOT1,并且必须在players.size() - 1结束,内循环必须从索引i+1开始。 en.wikipedia.org/wiki/Selection_sort#Implementation

标签: java arraylist selection-sort


【解决方案1】:

有几点:

  • 您正在初始化一个空的result 数组,但该算法通过交换元素对现有数组起作用。您必须使用 players 的值初始化 result

    List<Player> result = new ArrayList<Player>(players);
    
  • 变量temp 必须是Player 类型而不是int

    Player temp = result.get(smallIntIndex);
    
  • 外循环必须从索引0开始,不是1

  • 外循环必须在players.size() - 1结束
  • 内循环必须从索引i+1开始
  • 你每次都在外循环中交换元素,这是正确的。只有在找到新的最小值时才交换它们

更正后的代码:

public ArrayList<Player> ranking() {
    List<Player> result = new ArrayList<Player>(players);
    int smallInt = 0;
    int j=0;
    int smallIntIndex = 0;      

    for(int i=0;i<result.size() - 1;i++){
        smallInt = result.get(i).totalScore();
        smallIntIndex = i;
        for(j=i+1;j<result.size();j++){
            if(result.get(j).totalScore()<smallInt){
                smallInt = result.get(j).totalScore();
                smallIntIndex = j;                    
            }
        }

        if (i != smallIntIndex) {
            Player temp = result.get(smallIntIndex);
            result.set(smallIntIndex, result.get(i));
            result.set(i, temp);
        }
    }
    return result;
}

编辑:您要求排序后的结果必须放入一个单独的 results 数组,该数组最初为空。这是一种方法:

public ArrayList<Player> ranking() {
    List<Player> result = new ArrayList<Player>();
    int smallInt = 0;
    int j=0;
    int smallIntIndex = 0;      

    for(int i=0;i<players.size() - 1;i++){
        smallInt = players.get(i).totalScore();
        smallIntIndex = i;
        for(j=i+1;j<players.size();j++){
            if(players.get(j).totalScore()<smallInt){
                smallInt = players.get(j).totalScore();
                smallIntIndex = j;                    
            }
        }

        if (i != smallIntIndex) {
            Player player = players.get(smallIntIndex);

            result.add(player);
            players.set(smallIntIndex, players.get(i));
        }
    }
    return result;
}

参考:Wikipedia's article on selection sort

【讨论】:

  • 嗯。 result 只是在调用该方法时创建的,这是否意味着它与 ArrayList players 具有相同的值,然后对其进行排序?是否应该将值插入result,或者像if(result.get(j).totalScore()&lt;smallInt) 这样的行不应该包括result?还是我错了,result 包含与players 相同的值?
  • 恐怕它实际上并没有对它们进行排序。它只是以输入相同的顺序返回相同的值。但它没有给我一个编译错误,所以很好。
  • 哦等等,你是如何显示它们的?我正在使用System.out.println(T.ranking());,它输出[]?如何正确显示?
  • ArrayList&lt;Player&gt; winners2 = T.ranking(); for(int i = 0; i &lt;winners2.size(); i++) System.out.println(T.ranking()); 。像那样?对不起,我不太确定我明白你的意思
【解决方案2】:

如果你改变了

int temp = result.get(smallIntIndex).totalScore();

玩家温度 = result.get(smallIntIndex);

程序将编译。

原始代码尝试将PersonstotalScore 插入到列表中,而不是Person

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多