为什么我在Java中遇到这个编译错误?

时间:2022-08-22 22:52:00

I get the following error:

我收到以下错误:

quicksort(int[],int,int)cannot be applied to(int[])

When I compile this:

当我编译这个:

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]);

  }
 }
}

What am I doing wrong?

我究竟做错了什么?

6 个解决方案

#1


Just after the line to print out "before", you have:

在“之前”打印出来之后,你有:

quicksort (tab);

The function you designed needs three arguments. You can either add the extra arguments:

您设计的功能需要三个参数。您可以添加额外的参数:

quicksort (tab, 0, tab.length - 1)

Or add a new function such as:

或者添加一个新功能,例如:

public quicksort(int[]) {
  quicksort(tab, 0, tab.length - 1);
}

#2


the function "quicksort" that you define asks for 3 parameters, but you are only providing one.

你定义的函数“quicksort”要求3个参数,但你只提供一个。

#3


Because your quicksort function has 3 parameters, but your call gives only one.

因为您的快速排序功能有3个参数,但您的通话仅提供一个参数。

Edit: second :(

编辑:第二:(

#4


your code should call

你的代码应该调用

quicksort (tab,0,10);

In your outer calll, so you can sort the list.

在你的外部调用中,所以你可以对列表进行排序。

#5


Without knowing what you're writing code in, I strongly recommend using an IDE if you haven't adopted one already. Particularly Eclipse for Java.

在不知道你在编写代码的情况下,如果你还没有采用IDE,我强烈建议使用IDE。特别是Eclipse for Java。

Eclipse would underline the offending line of code and make some suggestions to you, (in addition to offering code completion). A text editor, like JEdit does not.

Eclipse会强调有问题的代码行并向您提出一些建议(除了提供代码完成)。像JEdit这样的文本编辑器没有。

Note: I've been told IntelliJ is good, but you can't beat Eclipse's price (free).

注意:我被告知IntelliJ很好,但你无法击败Eclipse的价格(免费)。

#6


BTW: You could just use Arrays.sort() which is a built in function. You wouldn't write a function like this in real life. (only as homework)

顺便说一句:你可以使用Arrays.sort()这是一个内置函数。在现实生活中你不会写这样的函数。 (仅作为家庭作业)

#1


Just after the line to print out "before", you have:

在“之前”打印出来之后,你有:

quicksort (tab);

The function you designed needs three arguments. You can either add the extra arguments:

您设计的功能需要三个参数。您可以添加额外的参数:

quicksort (tab, 0, tab.length - 1)

Or add a new function such as:

或者添加一个新功能,例如:

public quicksort(int[]) {
  quicksort(tab, 0, tab.length - 1);
}

#2


the function "quicksort" that you define asks for 3 parameters, but you are only providing one.

你定义的函数“quicksort”要求3个参数,但你只提供一个。

#3


Because your quicksort function has 3 parameters, but your call gives only one.

因为您的快速排序功能有3个参数,但您的通话仅提供一个参数。

Edit: second :(

编辑:第二:(

#4


your code should call

你的代码应该调用

quicksort (tab,0,10);

In your outer calll, so you can sort the list.

在你的外部调用中,所以你可以对列表进行排序。

#5


Without knowing what you're writing code in, I strongly recommend using an IDE if you haven't adopted one already. Particularly Eclipse for Java.

在不知道你在编写代码的情况下,如果你还没有采用IDE,我强烈建议使用IDE。特别是Eclipse for Java。

Eclipse would underline the offending line of code and make some suggestions to you, (in addition to offering code completion). A text editor, like JEdit does not.

Eclipse会强调有问题的代码行并向您提出一些建议(除了提供代码完成)。像JEdit这样的文本编辑器没有。

Note: I've been told IntelliJ is good, but you can't beat Eclipse's price (free).

注意:我被告知IntelliJ很好,但你无法击败Eclipse的价格(免费)。

#6


BTW: You could just use Arrays.sort() which is a built in function. You wouldn't write a function like this in real life. (only as homework)

顺便说一句:你可以使用Arrays.sort()这是一个内置函数。在现实生活中你不会写这样的函数。 (仅作为家庭作业)