简体   繁体   English

尝试使用对象调用方法,但无法找出要放入的参数

[英]Trying to call a method with an object, but can't figure out arguments to put

I have a method in the class EarthquakeDataSet called public void mergeSort() where I create an object and try to call a sort() method from my MergeSorter class. 我在EarthquakeDataSet类中有一个称为public void mergeSort()的方法,在其中创建一个对象,并尝试从MergeSorter类中调用sort()方法。 The MergeSorter class itself is not my code, it's written by someone else but I'm supposed to call the sort() and use this class that someone else has written. MergeSorter类本身不是我的代码,它是由其他人编写的,但是我应该调用sort()并使用其他人编写的此类。 The exact error I'm getting is: "The method sort(E[],Comparator ) in the type MergeSorter is not applicable for arguments" 我得到的确切错误是:“类型为MergeSorter的方法sort(E [],Comparator)不适用于参数”

My method in EarthquakeDataSet looks like this: 我在EarthquakeDataSet中的方法如下所示:

public void mergeSort(){
MergeSorter obj = new MergeSorter();
obj.sort();

} }

The method in MergeSorter I'm trying to call, calls another method, which also calls another method. 我尝试调用的MergeSorter中的方法调用了另一个方法,该方法也调用了另一个方法。 This is my MergeSorter class. 这是我的MergeSorter类。

The three methods in MergeSorter looks like this: MergeSorter中的三个方法如下所示:

public static <E> void sort(E[] a, Comparator<? super E> comp) {
     mergeSort(a, 0, a.length - 1, comp); //calling mergeSort method
   }

and

private static <E> void mergeSort(E[] a, int from, int to, Comparator<? super E> comp){
}

and

private static <E> void merge(E[] a, int from, int mid, int to, Comparator<? super E> comp) {

}

There's a fair bit of code inside but I'm just having trouble calling with these arguments. 里面有很多代码,但是我在调​​用这些参数时遇到了麻烦。

As shown by both the error message and the method code you've posted, the sort method takes two arguments - firstly an array of things to sort, and secondly a Comparator over those things to define the ordering. 如错误消息和您已发布的方法代码所示, sort方法采用两个参数-首先是要排序的事物数组,其次是通过Comparator这些事物以定义顺序。

You're trying to call sort() with no arguments, which isn't legal and won't compile. 您正在尝试不带任何参数的sort()调用,这是非法的,也不会编译。 (And even if it did compile - what would you expect this to do? What exactly would it be sorting?) (即使它编译了,您希望它做什么?它到底要排序什么?)

You'll possibly need to update your own mergeSort method to take the data to be sorted as a parameter. 您可能需要更新自己的mergeSort方法,以将要排序的数据作为参数。 This might not be necessary if it's a field in the class - but either way, you'll need to pass that data into MergeSorter.sort . 如果它是类中的一个字段,则可能没有必要-但无论哪种方式,您都需要将该数据传递到MergeSorter.sort

You're calling 你在打电话

obj.sort();

But the method signature looks like this: 但是方法签名看起来像这样:

public static <E> void sort(E[] a, Comparator<? super E> comp)

The error you're getting is telling you you're using the wrong arguments, or in this case, you're not using any arguments at all. 您得到的错误是告诉您您使用了错误的参数,或者在这种情况下,您根本没有使用任何参数。

An example of correct usage would be something like: 正确用法的示例如下:

String[] values = "Hello my name is chris".split(" ");
obj.sort(values, new Comparator<String>() {
    public int compare(String s1, String s2)
    {
         return s1.compareTo(s2);
    }
);

First of all, sort method of MergeSorter is static, so no need to create object to call the method, and then you are calling, obj.sort(), which clearly does not pass any argument to the method whose signature is 首先,MergeSorter的sort方法是静态的,因此无需创建对象来调用该方法,然后您将调用obj.sort(),它显然不会将任何参数传递给签名为的方法

public static <E> void sort(E[] a, Comparator<? super E> comp)

you must pass two arguments to the method, as in example 您必须将两个参数传递给方法,例如

    class Try<E> implements Comparator<E>
   {
      public int compare(E o1, E o2) {
     //logic here
     }

  }

public class Test {

    static Try<String>[] a = new Try[5];

    public static void main(String[] args) {
        sort(a, new Try());
    }

    public static <E> void sort(E[] a, Comparator<? super E> comp)
    {
        //sort logic
    }

  }

I am calling from main method, in you case you can call like MergeSorter.sort(a, new Try()); 我是从main方法调用的,在这种情况下,您可以像MergeSorter.sort(a,new Try())一样进行调用;

Hope I am clear. 希望我清楚。

You referred to the things you're sorting as "records". 您将要排序的内容称为“记录”。 Usually, when sorting records, the records have several data items but you want to sort on one particular item (eg in alphabetical order by city name); 通常,在对记录进行排序时,记录中有多个数据项,但是您希望对一个特定项进行排序(例如,按城市名称的字母顺序); or sometimes multiple items (eg alpha order by city name, and then for records in the same city, in ascending order by time or something). 或有时是多个项目(例如,按城市名称的字母顺序排列,然后用于同一城市中的记录,按时间或其他顺序升序排列)。 A comparator would look something like this: 比较器如下所示:

EarthquakeRecord[] values = <whatever>;
obj.sort (values, new Comparator<EarthquakeRecord>() {
    public int compare (EarthquakeRecord r1, EarthquakeRecord r2) {
         return r1.getCity().compareTo (r2.getCity());
    });

Or to sort on multiple items: 或对多个项目进行排序:

obj.sort (values, new Comparator<EarthquakeRecord>() {
    public int compare (EarthquakeRecord r1, EarthquakeRecord r2) {
         int cityCompare = r1.getCity().compareTo (r2.getCity());
         if (cityCompare != 0)
             return cityCompare;
         return Integer.compare (r1.getTime(), r2.getTime());
             // assuming getTime() returns the time as an integer, somehow
    });

Or to sort in descending order: 或以降序排列:

obj.sort (values, new Comparator<EarthquakeRecord>() {
    public int compare (EarthquakeRecord r1, EarthquakeRecord r2) {
         return Double.compare (r2.getMagnitude(), r1.getMagnitude());
         // this will also work:
         // return -Double.compare (r1.getMagnitude(), r2.getMagnitude());
    });

The compare and compareTo methods return a negative integer if the first argument is less, a positive integer if the first argument is larger, 0 if they're equal. 如果第一个参数较小,则comparecompareTo方法将返回负整数;如果第一个参数较大,则compare返回正整数;如果相等,则返回0。 That's all that's required for sort . 这就是sort需要的。

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

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