[英]Why do I get this compile error in Java?
我收到以下錯誤:
quicksort(int[],int,int)cannot be applied to(int[])
當我編譯這個:
import java.util.*;
public class Sort {
public static void main(String[] args){
Random rand = new Random();
int[] tab = new int[10];
for(int i = 0; i < tab.length; i++) {
tab[i] = rand.nextInt(100);
System.out.println("Before: ");
show(tab);
quicksort (tab);
System.out.println("After: ");
show(tab);
}
}
static void quicksort(int tab[], int x, int y) {
int i,j,v,temp;
i=x;
j=y;
v=tab[(x+y) / 2];
do {
while (tab[i]<v)
i++;
while (v<tab[j])
j--;
if (i<=j) {
temp=tab[i];
tab[i]=tab[j];
tab[j]=temp;
i++;
j--;
}
}
while (i<=j);
if (x<j)
quicksort(tab,x,j);
if (i<y)
quicksort(tab,i,y);
}
static void show (int tab[]) {
for (int i = 0; i <tab.length; i++) {
System.out.println(tab[i]);
}
}
}
我究竟做錯了什么?
在“之前”打印出來之后,你有:
quicksort (tab);
您設計的功能需要三個參數。 您可以添加額外的參數:
quicksort (tab, 0, tab.length - 1)
或者添加一個新功能,例如:
public quicksort(int[]) {
quicksort(tab, 0, tab.length - 1);
}
你定義的函數“quicksort”要求3個參數,但你只提供一個。
因為您的快速排序功能有3個參數,但您的通話僅提供一個參數。
編輯:第二:(
你的代碼應該調用
quicksort (tab,0,10);
在你的外部調用中,所以你可以對列表進行排序。
在不知道你在編寫代碼的情況下,如果你還沒有采用IDE,我強烈建議使用IDE。 特別是Eclipse for Java。
Eclipse會強調有問題的代碼行,並向您提出一些建議(除了提供代碼完成)。 像JEdit這樣的文本編輯器沒有。
注意:我被告知IntelliJ很好,但你無法擊敗Eclipse的價格(免費)。
BTW:您可以使用Arrays.sort()這是一個內置函數。 在現實生活中你不會寫這樣的函數。 (僅作為家庭作業)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.