4

私は次のコードを持っています。コンパイルしようとすると、次のエラーが発生します。

The method sort(List<T>, Comparator<? super T>) in the type Collections is not 
applicable for the arguments (Software[], new Comparator(){}) 
The type new Comparator(){} must implement the inherited abstract method
Comparator.compare(Object, Object)

コード

import java.text.DecimalFormat; // For proper currency  
import java.util.Comparator;
import java.util.Collections;


public class Software

{
// Declare variables

String SoftwareTitle; // SoftwareTitle
int SoftwareStock;    // Software totals
double SoftwarePrice; // Software Price
int SoftwareNum;      // Software Product ID
double CalculateInventory;  // To add inventory
double SoftwareValue;       // Software Total value
double value;               // Complete inventory total

Software( String softtitle, int softstock, double softprice, int softitemnum )

{
    // Create object constructor

    SoftwareTitle = softtitle;
    SoftwareStock = softstock;
    SoftwarePrice = softprice;
    SoftwareNum = softitemnum;

    } 

    // Set Software Title

    public void setSoftwareTitle( String softtitle )
    {
        SoftwareTitle = softtitle;
    } 


    // Return Software Title

    public String getSoftwareTitle()
    {
        return SoftwareTitle;
    } 

    // Set software inventory
    public void setSoftwareStock( int softstock)
    {
        SoftwareStock = softstock;
    } 

    // Return software inventory
    public int getSoftwareStock()
    {
        return SoftwareStock;
    }

    // Set software price

    public void setSoftwarePrice( double softprice )
    {
        SoftwarePrice = softprice;
    }

    // Return software price
    public double getSoftwarePrice()
    {
        return SoftwarePrice;

    }

    // Set item number

    public void setSoftwareNum( int softitemnum )
    {
        SoftwareNum = softitemnum;
          }         //

    //return software item number

    public int getSoftwareNum()
    {
        return SoftwareNum;
    } //


    // calculate inventory value

    public double Softwarevalue()
    {
        return SoftwarePrice * SoftwareStock;

    } 

    public void setCalculateInventory (double value){
        this.CalculateInventory = value;
    }



    public double getCalculateInventory(){
        double value = 0;
        for(int i = 0; i < 3; i++){
            value = Softwarevalue();
        }
        return value;
    }


 }//end method value

 //

import java.text.DecimalFormat; // For proper currency  
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;


 public class Inventory {

public static void main( String args[] )

{

    // Start array of software titles

    Software[] aSoftware = new Software[4];

    aSoftware[0]= new Software("Command and Conquer ", 6, 29.99, 10122); 
        aSoftware[1]= new Software("Alice in Wonderland", 1, 10.99,10233);
        aSoftware[2]= new Software("Doom", 1, 10.99, 10344);
    aSoftware[3]= new Software("Walking Dead", 6, 9.99, 10455);

//Set currency format
    DecimalFormat money = new DecimalFormat("$0.00");

// Sort in order of Software Name

    Collections.sort(aSoftware, new Comparator() {
    public int compare(Software s1, Software s2) {
    return s1.getSoftwareTitle().compareTo(s2.getSoftwareTitle());

                }
            });



// Display software title, number of units, cost, item number and total inventory

     for (int i = 0; i < aSoftware.length; i++){    

System.out.println("Software Title is "+ aSoftware[i].getSoftwareTitle() );

System.out.println("The number of units in stock is "+    aSoftware[i].getSoftwareStock() );

System.out.println("The price of the Software Title is "+ (money.format(aSoftware[i].getSoftwarePrice() )));

System.out.println( "The item number is "+ aSoftware[i].getSoftwareNum());

System.out.println( "The value of the Software Inventory is "+  (money.format(aSoftware[i].Softwarevalue() )));

System.out.println();   
            }

//output total inventory value

double total = 0.0;
    for (int i = 0; i < 3; i++){ 
    total +=  aSoftware[i].getCalculateInventory();            
        }
    System.out.printf("Total Value of Software Inventory is: \t$%.2f\n", total);


//end output total inventory value








 }
 }

 //

 //end 

コンパレータを使用してソフトウェアタイトル(配列)をアルファベット順に表示するにはどうすればよいですか?

4

5 に答える 5

6

2 つの問題があります。

1)Collections.sort(を使用します)を使用していList<E>ますが、配列をソートしようとしています。Arrays.sort代わりに使用してください。

Comparator<Software>2)生のComparator型だけでなく、実装していることを指定する必要があります。

基本的に、これは機能します:

Arrays.sort(aSoftware, new Comparator<Software>() {
    public int compare(Software s1, Software s2) {
        return s1.getSoftwareTitle().compareTo(s2.getSoftwareTitle());
    }
});
于 2012-11-02T19:16:35.683 に答える
3

まず、 などの配列をソートするには、ではなくSoftware[]を使用する必要があります。java.util.Arrays.sortjava.util.Collections.sort

第二に、 yourComparatorは特にSoftwareインスタンス用であるためnew Comparator<Software>()、単にではなく書く必要がありますnew Comparator()。(後者は実際には動作する場合でも悪いコードです。)

于 2012-11-02T19:16:08.787 に答える
0

オブジェクトの配列を使用しようとしているため、以下を使用します。

Arrays.sort(aSoftware);

また、ソフトウェア クラスは implements Comparable を実装し、その compareTo メソッドをオーバーライドする必要があります。

@Override
public int compareTo(Software o) {
    return this.getSoftwareTitle().compareTo(o.getSoftwareTitle());
}

以下のようにクラスを修正しました。

public class Software implements Comparable<Software>{
// Declare variables

String SoftwareTitle; // SoftwareTitle
int SoftwareStock; // Software totals
double SoftwarePrice; // Software Price
int SoftwareNum; // Software Product ID
double CalculateInventory; // To add inventory
double SoftwareValue; // Software Total value
double value; // Complete inventory total

Software(){

}

Software(String softtitle, int softstock, double softprice, int softitemnum)

{
    // Create object constructor

    SoftwareTitle = softtitle;
    SoftwareStock = softstock;
    SoftwarePrice = softprice;
    SoftwareNum = softitemnum;

}

// Set Software Title

public void setSoftwareTitle(String softtitle) {
    SoftwareTitle = softtitle;
}

// Return Software Title

public String getSoftwareTitle() {
    return SoftwareTitle;
}

// Set software inventory
public void setSoftwareStock(int softstock) {
    SoftwareStock = softstock;
}

// Return software inventory
public int getSoftwareStock() {
    return SoftwareStock;
}

// Set software price

public void setSoftwarePrice(double softprice) {
    SoftwarePrice = softprice;
}

// Return software price
public double getSoftwarePrice() {
    return SoftwarePrice;

}

// Set item number

public void setSoftwareNum(int softitemnum) {
    SoftwareNum = softitemnum;
} //

// return software item number

public int getSoftwareNum() {
    return SoftwareNum;
} //

// calculate inventory value

public double Softwarevalue() {
    return SoftwarePrice * SoftwareStock;

}

public void setCalculateInventory(double value) {
    this.CalculateInventory = value;
}

public double getCalculateInventory() {
    double value = 0;
    for (int i = 0; i < 3; i++) {
        value = Softwarevalue();
    }
    return value;
}

@Override
public int compareTo(Software o) {
    return this.getSoftwareTitle().compareTo(o.getSoftwareTitle());
}





}// end method value

あなたの在庫クラス:

public class Inventory {

public static void main(String args[])

{

    // Start array of software titles

    Software[] aSoftware = new Software[4];

    aSoftware[0] = new Software("Command and Conquer ", 6, 29.99, 10122);
    aSoftware[1] = new Software("Alice in Wonderland", 1, 10.99, 10233);
    aSoftware[2] = new Software("Doom", 1, 10.99, 10344);
    aSoftware[3] = new Software("Walking Dead", 6, 9.99, 10455);

    // Set currency format
    DecimalFormat money = new DecimalFormat("$0.00");

    Arrays.sort(aSoftware);



    // Display software title, number of units, cost, item number and total
    // inventory

    for (int i = 0; i < aSoftware.length; i++) {

        System.out.println("Software Title is "
                + aSoftware[i].getSoftwareTitle());

        System.out.println("The number of units in stock is "
                + aSoftware[i].getSoftwareStock());

        System.out.println("The price of the Software Title is "
                + (money.format(aSoftware[i].getSoftwarePrice())));

        System.out.println("The item number is "
                + aSoftware[i].getSoftwareNum());

        System.out.println("The value of the Software Inventory is "
                + (money.format(aSoftware[i].Softwarevalue())));

        System.out.println();
    }

    // output total inventory value

    double total = 0.0;
    for (int i = 0; i < 3; i++) {
        total += aSoftware[i].getCalculateInventory();
    }
    System.out.printf("Total Value of Software Inventory is: \t$%.2f\n",
            total);

    // end output total inventory value

}
}

以下は、ソートされた順序での最終出力です。

Software Title is Alice in Wonderland
The number of units in stock is 1
The price of the Software Title is $10.99
The item number is 10233
The value of the Software Inventory is $10.99

Software Title is Command and Conquer 
The number of units in stock is 6
The price of the Software Title is $29.99
The item number is 10122
The value of the Software Inventory is $179.94

Software Title is Doom
The number of units in stock is 1
The price of the Software Title is $10.99
The item number is 10344
The value of the Software Inventory is $10.99

Software Title is Walking Dead
The number of units in stock is 6
The price of the Software Title is $9.99
The item number is 10455
The value of the Software Inventory is $59.94

Total Value of Software Inventory is:   $201.92
于 2014-04-06T22:55:17.453 に答える
0

を使用している場合、配列でソートすることはできませんCollections.sortCollections.sortリストのみを受け入れます。ユーザーArrays.sortではなくCollection.sort

于 2012-11-02T19:16:48.557 に答える
-1

「ソフトウェア」クラスを比較可能に実装し、比較メソッドを上書きして、コード外で行ったようにタイトルの比較を返す必要があります。これはコンパレーターの代わりになります。あとは、Arrays.sort を呼び出すだけです。

于 2012-11-02T19:15:08.663 に答える