如何修复我的sortData和compareTo方法。我不断收到“无法转换为错误”。

时间:2019-04-05 12:51:17

标签: java

我一直收到错误:不兼容的类型:当我尝试将数组传递给我的compareTo方法时,BingoPlayer无法转换为BingoPlayer []。我不太确定如何解决此问题。

我正在从文本文件读取数据。这是该文件的内容:

  • 50.00
  • 10
  • 詹姆斯,史密斯,50.0
  • Michael,Smith,50.0
  • Robert,Smith,50.0
  • 玛丽亚,加西亚,50.0
  • David,Smith,50.0
  • 玛丽亚,罗德里格斯,50.0
  • 玛丽,史密斯,50.0
  • 玛丽亚,赫尔南德斯,50.0
  • Maria,Martinez,50.0
  • James,Clapper,50.0

导入java.io. ; 导入java.util。;

public static void sortData()
{
    int n = test.length;
    int m =0;
    BingoPlayer temp;  
     for(int i=0; i < n; i++)
     {  
        for(int j=1; j < (n-i); j++)
        {  
            m = compareTo((test[j], test[j+1];
           if(m > 0)
           {  
             temp = test[j-1];  
             test[j-1] = test[j];  
             test[j] = temp;  
           }  

        }  
     }  

}
public static int compareTo(BingoPlayer[] player1, BingoPlayer[] player2)
{
    for(int i =0; i < 10; i++)
    {
            if(player1[i].firstName.compareTo(player2[i].firstName) != 0)
                return player1[i].firstName.compareTo((player2[i].firstName));
            else if (player1[i].lastName.compareTo(player2[i].lastName) != 0)
                return player1[i].lastName.compareTo((player2[i].lastName));
            else 
                return player1[i].lastName.compareTo(player2[i].lastName);
    }

}

}

2 个答案:

答案 0 :(得分:3)

由于您没有提供所有代码,因此我将假设“测试”是BingoPlayer对象的数组。

我建议您更改compareTo方法,以仅将1个BingoPlayer与另一个BingoPlayer进行比较。通过代码的工作方式,您可以比较测试中的所有内容,然后调用一种方法来再次比较测试中的所有内容。让我们减少冗余并尝试使其工作:

public static void sortData()
{
    int n = test.length;
    int m =0;
    BingoPlayer temp;  
     for(int i=0; i < n; i++)
     {  
        for(int j=1; j < (n-i); j++)
        {  
            m = compareTo((test[j], test[j+1];
           if(m > 0)
           {  
             temp = test[j-1];  
             test[j-1] = test[j];  
             test[j] = temp;  
           }  

        }  
     }  

}
public static int compareTo(BingoPlayer player1, BingoPlayer player2)
{

            if(player1.firstName.compareTo(player2.firstName) != 0)
                return player1.firstName.compareTo((player2.firstName));
            else if (player1.lastName.compareTo(player2.lastName) != 0)
                return player1.lastName.compareTo((player2.lastName));
            else 
                return player1.lastName.compareTo(player2.lastName);

}

}

同样,我不得不假设“测试”是一个BingoPlayer对象的数组。如果真是这样,那么您正在尝试将两个单个BingoPlayer对象发送到期望有两个BingoPlayer数组的方法。如上所示,对您的compareTo进行更改应该可以修复您的程序。

答案 1 :(得分:2)

您的compare方法应该使用两个BingoPlayer对象,而不是这些对象的数组。

您还应该阅读Comparable和Comparator接口。

编辑:Comparable和Comparator实现有一些代码段

class BingoPlayer implements Comparable<BingoPlayer> {

    // rest of your class
    String firstName, lastName;

    @Override
    public int compareTo(BingoPlayer player) {
        if(firstName.compareTo(player.firstName) != 0)
            return firstName.compareTo((player.firstName));
        else if (lastName.compareTo(player.lastName) != 0)
            return lastName.compareTo((player.lastName));
        else
            return lastName.compareTo(player.lastName);
    }
}

您可以这样使用它:

player1.compareTo(player2);

其他方法是创建自己的Comparator类:

class BingoPlayerComparator implements Comparator<BingoPlayer> {

    @Override
    public int compare(BingoPlayer player1, BingoPlayer player2) {
        if(player1.firstName.compareTo(player2.firstName) != 0)
            return player1.firstName.compareTo((player2.firstName));
        else if (player1.lastName.compareTo(player2.lastName) != 0)
            return player1.lastName.compareTo((player2.lastName));
        else 
            return player1.lastName.compareTo(player2.lastName);
    }
}

可以这样使用:

BingoPlayerComparator c = new BingoPlayerComparator();
c.compare(player1, player2);