我已經做了Java程序添加的航班信息的ArrayList的元素,我想基於使用比較航班名稱進行排序操作。但我得到的FLight.java編譯錯誤,在這條線.... Collections.sort(flightObject,新Compareid());使用可比以按對象
package com.flight;
import java.util.*;
public class Flight
{
public String flightName;
public String source;
public String destination;
public int flightId;
public int flightFare;
public Flight(String flightName,int flightId,String source, String destination, int flightFare)
{
super();
this.flightName = flightName;
this.source = source;
this.destination = destination;
this.flightId = flightId;
this.flightFare = flightFare;
}
public String getFlightName()
{
return flightName;
}
public int getFlightId()
{
return flightId;
}
public String getSource() {
return source;
}
public String getDestination() {
return destination;
}
public int getFlightFare() {
return flightFare;
}
public boolean searchFlight(int originalFlightId, int newFlightId)
{
if(originalFlightId==newFlightId)
{
return true;
}
else
{
return false;
}
}
class Compareid implements Comparator<Flight>{
public int compare(Flight flightObject1,Flight flightObject2){
return (flightObject1.flightName.compareTo (flightObject2.flightName));
}
}
}
這是主類.............
package com.main;
import com.flight.*;
import java.util.*;
public class HighFly
{
public static void main(String[] args)
{
Flight indigo=new Flight("Indigo",1000,"Trivandrum","Dubai",25000);
Flight emirates=new Flight("Emirates",1001,"Dehi","US",30000);
Flight airindia=new Flight("Air India",1002,"Kochi","Malaysia",29000);
ArrayList<Flight>flightList=new ArrayList<Flight>();
flightList.add(indigo);
flightList.add(emirates);
flightList.add(airindia);
System.out.println("FlightId FlightName Source Destination Fare");
for(Flight flightObject:flightList)
{
System.out.print(flightObject.getFlightId());
System.out.print("\t\t"+flightObject.getFlightName());
System.out.print("\t\t"+flightObject.getSource());
System.out.print("\t\t"+flightObject.getDestination());
System.out.println("\t\t"+flightObject.getFlightFare());
}
//SEARCHING FLIGHT EXISTS
System.out.println("Enter flight id");
Scanner scanner= new Scanner(System.in);
int checkFlightId=scanner.nextInt();
for(Flight flightObject:flightList)
{
if(flightObject.searchFlight(flightObject.flightId,checkFlightId))
{
System.out.println("Success");
}
else
{
System.out.println("Not success");
}
}
//*********sorting***********
for(Flight flightObject:flightList)
{
Collections.sort(flightObject,new Compareid());
}
System.out.println(" after sorting ");
System.out.println("FlightId FlightName Source Destination Fare");
for(Flight flightObject:flightList)
{
System.out.print(flightObject.getFlightId());
System.out.print("\t\t"+flightObject.getFlightName());
System.out.print("\t\t"+flightObject.getSource());
System.out.print("\t\t"+flightObject.getDestination());
System.out.println("\t\t"+flightObject.getFlightFare());
}
}
}
幫我解決這個問題。我想用arraylist來實現這個。請在以下代碼中找到該錯誤。
和錯誤是... –
並請格式化你的代碼。你的縮進遍佈整個地方,很難閱讀。 –
你得到什麼錯誤?請添加它在你的questiion – byteC0de