简体   繁体   English

选择排序方法错误,类型不兼容

[英]Selection Sort method error, incompatible types

Here is my code so far. 到目前为止,这是我的代码。 I am getting an incompatible type in the first for statement. 我在第一个for语句中得到了一个不兼容的类型。 Required: boolean Found: int 必需:布尔值找到:int

Not quite sure what I need to do to fix this. 不太确定我需要做什么来解决此问题。

public void selectionSort(int[] list){
    //implement selection sort here.  
    for (int i = 0;list.length -1;i++){
        int smvi = i;
        for(int j = i+1;j<list.length;j++){
            if(list[j] < list[smvi]){
                smvi=j;}
            if(i!=smvi){
                int temp = list[i];
                list[i] = list[smvi];
                list[smvi] = temp;
            }
        }
    }
}

The problem is here: list.length -1 问题在这里: list.length -1

What I think you want is i <= list.length - 1 我想你想要的是i <= list.length - 1

The Error clearly states that it requires boolean type but found int. 该错误明确指出它需要布尔类型,但找到int。

list.length -1;

will not return any boolean values, it simply returns a integer. 不会返回任何布尔值,它只会返回一个整数。 You too know that for(;condition;) syntax. 您也知道for(; condition;)语法。 Every condition will decide only based on boolean values. 每个条件将仅基于布尔值来决定。

You should change it to 您应该将其更改为

i < list.length -1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM