Java 错误:无法从 void 转换为 int[]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13382619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:31:20  来源:igfitidea点击:

Java Error: Cannot convert from void to int[]

java

提问by rharrison33

I don't understand why java thinks the array "thisRow" is void when it is passed into Arrays.sort(thisRow). "thisRow" appears to be an int[] to me. What is the issue here?

我不明白为什么 java 认为数组“thisRow”在传递到 Arrays.sort(thisRow) 时为空。“thisRow”对我来说似乎是一个 int[] 。这里有什么问题?

Error Message: "Type mismatch: cannot convert from void to int[] at Test.mySort(Test.java:57)"

错误消息:“类型不匹配:无法在 Test.mySort(Test.java:57) 处从 void 转换为 int[]”

private static int[][] mySort(int[][] anArray) {
    for(int i = 0; i < anArray.length; i++){
        int thisRow[] = getRow(anArray, i);
        int[] sorted = Arrays.sort(thisRow);
    }
}

//This method will get the specified row out of the array.
private static int[] getRow(int[][] anArray, int row) {
    int thisRow[] = new int[anArray[row].length];
    for(int j = 0; j < anArray[row].length; j++){
        thisRow[j] = anArray[row][j];
    }
    return thisRow;
}

回答by kosa

Arrays.sortreturns voidnot int[]type.

Arrays.sort返回的void不是int[]类型。

As per javadoc

根据 javadoc

Sorts the specified array of ints into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance

将指定的整数数组按数字升序排序。排序算法是一个经过调整的快速排序,改编自 Jon L. Bentley 和 M. Douglas McIlroy 的“设计排序功能”,软件实践和经验,卷。23(11) P. 1249-1265(1993 年 11 月)。该算法在许多数据集上提供 n*log(n) 性能,导致其他快速排序降级为二次性能

Replace

代替

int[] sorted = Arrays.sort(thisRow);

with

Arrays.sort(thisRow);

回答by Andrzej Doyle

Arrays.sortsorts the array in-place (by mutating the existing object), and doesn't return anything. Hence you should replace

Arrays.sort就地排序数组(通过改变现有对象),并且不返回任何内容。因此你应该更换

int[] sorted = Arrays.sort(thisRow);

with simply

简单地

Arrays.sort(thisRow);

回答by Subhrajyoti Majumder

Arrays#sortdoes not return int[], it returns void, it sort source array itself.

Arrays#sort不返回int[],它返回void,它对源数组本身进行排序。

Sorts the specified array of ints into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.

将指定的整数数组按数字升序排序。排序算法是一个经过调整的快速排序,改编自 Jon L. Bentley 和 M. Douglas McIlroy 的“设计排序功能”,软件实践和经验,卷。23(11) P. 1249-1265(1993 年 11 月)。该算法在许多数据集上提供 n*log(n) 性能,导致其他快速排序降级为二次性能。