Java - 尝试使用自定义比较器对 LinkedList 进行排序,找不到符号排序

发布于 2024-10-27 01:43:18 字数 9298 浏览 2 评论 0原文

我一直在尝试创建一些方法,以至少 4 种不同的方式对链接列表进行排序。但我不断收到编译器错误

MyBikeList.java:80: cannot find symbol
symbol  : method sort(java.util.List<Bike>,Comparator<Bike>)
location: class java.util.Collections
                Collections.sort(bikeInventory, byYear);

我的代码如下(不完整)

    public class MyBikeList implements BikeList
{

    List<Bike> bikeInventory = new LinkedList();
    public static Comparator<Bike> byPrice = new PriceComparator();
    public static Comparator<Bike> byYear = new YearComparator();
    public static Comparator<Bike> byModel = new ModelComparator();
    public static Comparator<Bike> byReg = new RegComparator();

    public boolean add(Bike bike)
    {
    {

    }

    public boolean remove(Bike bike)
    {
    }

    public Bike find(String regNo)
    {
        ListNode itr = header.next;

        while( itr != null && !itr.element.equals( Bike.bikeRegNO ) )
        {
            itr = itr.next;
        }

        return new LinkedListIterator( itr );
    }

    public int sortByRegNo()
    {
        Collections.sort(bikeInventory, byReg);
        return byReg.getCount();
    }

    public int sortByPrice()
    {
        Collections.sort(bikeInventory, byPrice);
        return byPrice.getCount();

    }

    public int sortByModel()
    {
        Collections.sort(bikeInventory, byModel);
        return byModel.getCount();

    }

    public int sortByYear()
    {
        Collections.sort(bikeInventory, byYear);
        return byYear.getCount();

    }
    public void clear()
    {
        bikeInventory.clear();
    }

    public int size()
    {
        int s = bikeInventory.size();
        return s;
    }
}



   import java.util.*;

public interface Comparator<T>
{

    int compare(T o1, T o2);
    int getCount();
}

    import java.util.*;

public class PriceComparator implements Comparator<Bike>
{

    private int count;


    public int compare(Bike b1, Bike b2)
    {
        int b1Pr = b1.getPrice();
        int b2Pr = b2.getPrice();

        if(b1Pr > b2Pr)
        {
            count++;
            return 1;
        }
        if(b1Pr == b2Pr)
        {
            return 0;
        }

        return -1;
    }

    public int getCount()
    {
        return count;
    }
}

import java.util.*;

public class YearComparator implements Comparator<Bike>
{

    private int count = 0;

    public int compare(Bike b1, Bike b2)
    {
        int b1Yr = b1.getYear();
        int b2Yr = b2.getYear();


        if(b1Yr > b2Yr)
        {
            count++;
            return 1;
        }
        else
        if(b1Yr == b2Yr)
        {
            return 0;
        }

        return -1;
    }

    public int getCount()
    {
        return count;
    }

}

import java.util.*;

public class RegComparator implements Comparator<Bike>
{

    private int count;


    public int compare(Bike b1, Bike b2)
    {
        String b1Reg = b1.getRegNo();
        String b2Reg = b2.getRegNo();

        int result = b1Reg.compareTo(b2Reg);

        if(result > 0)
        {
            count++;
            return 1;
        }
        if(result == 0)
        {
            return 0;
        }

        return -1;
    }

    public int getCount()
    {
        return count;
    }

}

import java.util.*;

public class ModelComparator implements Comparator<Bike>
{

    private int count;

    public int compare(Bike b1, Bike b2)
    {
        String b1Model = b1.getModel();
        String b2Model = b2.getModel();

        int result = b1Model.compareTo(b2Model);

        if(result > 0)
        {
            count++;
            return 1;
        }
        if(result == 0)
        {
            return 0;
        }

        return -1;
    }

    public int getCount()
    {
        return count;
    }
}

public class MyBike implements Bike
{

  private String bikeModel;
  private String bikeRegNo;
  private String bikeYear;
  private String bikePrice;
  private int counter;

  public MyBike (String model, String regNo, String year, String price)
  {
        bikeModel = model;
        bikeRegNo = regNo;
        bikeYear = year;
        bikePrice = price;
        counter = 0;
    }


    public String getModel()
    {
        return bikeModel;
    }

    public String getRegNo()
    {
        return bikeRegNo;
    }

    public int getYear()
    {
        int aYear = toInt(bikeYear);
        return aYear;
    }

    public int getPrice()
    {
        int aPrice = toInt(bikePrice);
        return aPrice;
    }

    public void setPrice(int newPrice)
    {

        bikePrice = "" + newPrice;
    }

    public int getCompCount()
    {
        int tempInt = counter;
        counter = 0;
        return tempInt;
    }

    public boolean match(String model, int year, int price)
    {
        boolean nearMatch = false;
        int pricePercent = getPrice() /10;  
        if(model != null)
        {
            nearMatch = true;
        }

        if((year > 0) && (year >= getYear() -1) && (year <= getYear() + 1))
        {
            nearMatch = true;
        }

        if((price > 0) && (price >= getPrice() - pricePercent) && (price <= getPrice() + pricePercent))
        {
            nearMatch = true;
        }

        if(nearMatch == true)
        {
            counter++;
        }

        return nearMatch;


    }

    public String toString()
    {
        String aString = ("" + getPrice() + "   " + getYear()
                                + " " + getRegNo() + "  " +
                                getModel());
        return aString;
    }

    public int toInt(String s)
    {
        int anInt = Integer.parseInt(s);
        return anInt;
    }

    public int compareTo(Bike b)
    {
        int result = getRegNo().compareTo(b.getRegNo());
        if (result < 0)
        {
            return -1;
        }
        if (result == 0)
        {
            return 0;
        }

        return 1;

    }   

    public void compare(Bike b)
    {
    }

}


public interface Bike extends Comparable<Bike>
{
    /**
     * Get the bike's model description
     * @return  the model
     */
    public String getModel();

    /**
     * Get the bike's registration number
     * @return  the regNo
     */
    public String getRegNo();

    /**
     * Get the bike's year of registration
     * @return  the year
     */
    public int getYear();

    /**
     * Get the bike's price
     * @return  the price
     */
    public int getPrice();

    /**
     * Set the bike's price
     * @param price  the price
     */
    public void setPrice(int price);

    /**
     * Get & reset the comparison counter value
     * @return  the count (before resetting to zero)
     */
    public int getCompCount();

    /**
     * Check for "near match" (see assignment brief for details)
     * @param model  ignore if null/empty, else match as substring, 
     * if substring is engine size e.g. "800cc", remove number substring 
     * and convert to a number and match to within 50
     * @param year   ignore if <= 0, else match to within 1 yr
     * @param price  ignore if <= 0, else match to within 10%
     * @return  true if "near match" as above, else false
     */
    public boolean match(String model, int year, int price);

    /**
     * Create a string with price, year, regNo & model description
     * concatenated in that order, separated by single tabs
     * @return  a printable string as above 
     */
    public String toString();
}
    public interface BikeList
{
    /**
     * Add a given bike object to the list
     * @param bike  the bike object to add
     * @return  true if successful, else false
     */
    public boolean add(Bike bike);

    /**
     * Remove a bike object from the list
     * @param bike  the bike object to remove
     * @return  true if successful, else false
     */
    public boolean remove(Bike bike);

    /**
     * Find a bike given its registration number
     * @param regNo  the registration number
     * @return  the bike object, or null if not found
     */
    public Bike find(String regNo);

    /**
     * Sort by registration number
     * @return  the number of comparisons
     */
    public int sortByRegNo();

    /**
     * Sort by registration year
     * @return  the number of comparisons
     */
    public int sortByYear();

    /**
     * Sort by price
     * @return  the number of comparisons
     */
    public int sortByPrice();

    /**
     * Sort by model description
     * @return  the number of comparisons
     */
    public int sortByModel();

    /**
     * Clear the list
     */
    public void clear();

    /**
     * Get the size of the list (= number of bikes)
     * @return  the list size
     */
    public int size();

    /**
     * Convert list to single string with \n after each record
     * @return the string as above
     */
    public String toString();

    /**
     * Get an iterator object to traverse the bike list
     * @return  a java.util.Iterator<Bike> object
     */
    public java.util.Iterator<Bike> iterator();
}

提前谢谢您,抱歉长度太长,我不确定有多重要。

谢谢!

I've been trying to make some methods that will sort a linked list by at least 4 different ways. But I keep getting the compiler error

MyBikeList.java:80: cannot find symbol
symbol  : method sort(java.util.List<Bike>,Comparator<Bike>)
location: class java.util.Collections
                Collections.sort(bikeInventory, byYear);

My code is as follows (It's not complete)

    public class MyBikeList implements BikeList
{

    List<Bike> bikeInventory = new LinkedList();
    public static Comparator<Bike> byPrice = new PriceComparator();
    public static Comparator<Bike> byYear = new YearComparator();
    public static Comparator<Bike> byModel = new ModelComparator();
    public static Comparator<Bike> byReg = new RegComparator();

    public boolean add(Bike bike)
    {
    {

    }

    public boolean remove(Bike bike)
    {
    }

    public Bike find(String regNo)
    {
        ListNode itr = header.next;

        while( itr != null && !itr.element.equals( Bike.bikeRegNO ) )
        {
            itr = itr.next;
        }

        return new LinkedListIterator( itr );
    }

    public int sortByRegNo()
    {
        Collections.sort(bikeInventory, byReg);
        return byReg.getCount();
    }

    public int sortByPrice()
    {
        Collections.sort(bikeInventory, byPrice);
        return byPrice.getCount();

    }

    public int sortByModel()
    {
        Collections.sort(bikeInventory, byModel);
        return byModel.getCount();

    }

    public int sortByYear()
    {
        Collections.sort(bikeInventory, byYear);
        return byYear.getCount();

    }
    public void clear()
    {
        bikeInventory.clear();
    }

    public int size()
    {
        int s = bikeInventory.size();
        return s;
    }
}



   import java.util.*;

public interface Comparator<T>
{

    int compare(T o1, T o2);
    int getCount();
}

    import java.util.*;

public class PriceComparator implements Comparator<Bike>
{

    private int count;


    public int compare(Bike b1, Bike b2)
    {
        int b1Pr = b1.getPrice();
        int b2Pr = b2.getPrice();

        if(b1Pr > b2Pr)
        {
            count++;
            return 1;
        }
        if(b1Pr == b2Pr)
        {
            return 0;
        }

        return -1;
    }

    public int getCount()
    {
        return count;
    }
}

import java.util.*;

public class YearComparator implements Comparator<Bike>
{

    private int count = 0;

    public int compare(Bike b1, Bike b2)
    {
        int b1Yr = b1.getYear();
        int b2Yr = b2.getYear();


        if(b1Yr > b2Yr)
        {
            count++;
            return 1;
        }
        else
        if(b1Yr == b2Yr)
        {
            return 0;
        }

        return -1;
    }

    public int getCount()
    {
        return count;
    }

}

import java.util.*;

public class RegComparator implements Comparator<Bike>
{

    private int count;


    public int compare(Bike b1, Bike b2)
    {
        String b1Reg = b1.getRegNo();
        String b2Reg = b2.getRegNo();

        int result = b1Reg.compareTo(b2Reg);

        if(result > 0)
        {
            count++;
            return 1;
        }
        if(result == 0)
        {
            return 0;
        }

        return -1;
    }

    public int getCount()
    {
        return count;
    }

}

import java.util.*;

public class ModelComparator implements Comparator<Bike>
{

    private int count;

    public int compare(Bike b1, Bike b2)
    {
        String b1Model = b1.getModel();
        String b2Model = b2.getModel();

        int result = b1Model.compareTo(b2Model);

        if(result > 0)
        {
            count++;
            return 1;
        }
        if(result == 0)
        {
            return 0;
        }

        return -1;
    }

    public int getCount()
    {
        return count;
    }
}

public class MyBike implements Bike
{

  private String bikeModel;
  private String bikeRegNo;
  private String bikeYear;
  private String bikePrice;
  private int counter;

  public MyBike (String model, String regNo, String year, String price)
  {
        bikeModel = model;
        bikeRegNo = regNo;
        bikeYear = year;
        bikePrice = price;
        counter = 0;
    }


    public String getModel()
    {
        return bikeModel;
    }

    public String getRegNo()
    {
        return bikeRegNo;
    }

    public int getYear()
    {
        int aYear = toInt(bikeYear);
        return aYear;
    }

    public int getPrice()
    {
        int aPrice = toInt(bikePrice);
        return aPrice;
    }

    public void setPrice(int newPrice)
    {

        bikePrice = "" + newPrice;
    }

    public int getCompCount()
    {
        int tempInt = counter;
        counter = 0;
        return tempInt;
    }

    public boolean match(String model, int year, int price)
    {
        boolean nearMatch = false;
        int pricePercent = getPrice() /10;  
        if(model != null)
        {
            nearMatch = true;
        }

        if((year > 0) && (year >= getYear() -1) && (year <= getYear() + 1))
        {
            nearMatch = true;
        }

        if((price > 0) && (price >= getPrice() - pricePercent) && (price <= getPrice() + pricePercent))
        {
            nearMatch = true;
        }

        if(nearMatch == true)
        {
            counter++;
        }

        return nearMatch;


    }

    public String toString()
    {
        String aString = ("" + getPrice() + "   " + getYear()
                                + " " + getRegNo() + "  " +
                                getModel());
        return aString;
    }

    public int toInt(String s)
    {
        int anInt = Integer.parseInt(s);
        return anInt;
    }

    public int compareTo(Bike b)
    {
        int result = getRegNo().compareTo(b.getRegNo());
        if (result < 0)
        {
            return -1;
        }
        if (result == 0)
        {
            return 0;
        }

        return 1;

    }   

    public void compare(Bike b)
    {
    }

}


public interface Bike extends Comparable<Bike>
{
    /**
     * Get the bike's model description
     * @return  the model
     */
    public String getModel();

    /**
     * Get the bike's registration number
     * @return  the regNo
     */
    public String getRegNo();

    /**
     * Get the bike's year of registration
     * @return  the year
     */
    public int getYear();

    /**
     * Get the bike's price
     * @return  the price
     */
    public int getPrice();

    /**
     * Set the bike's price
     * @param price  the price
     */
    public void setPrice(int price);

    /**
     * Get & reset the comparison counter value
     * @return  the count (before resetting to zero)
     */
    public int getCompCount();

    /**
     * Check for "near match" (see assignment brief for details)
     * @param model  ignore if null/empty, else match as substring, 
     * if substring is engine size e.g. "800cc", remove number substring 
     * and convert to a number and match to within 50
     * @param year   ignore if <= 0, else match to within 1 yr
     * @param price  ignore if <= 0, else match to within 10%
     * @return  true if "near match" as above, else false
     */
    public boolean match(String model, int year, int price);

    /**
     * Create a string with price, year, regNo & model description
     * concatenated in that order, separated by single tabs
     * @return  a printable string as above 
     */
    public String toString();
}
    public interface BikeList
{
    /**
     * Add a given bike object to the list
     * @param bike  the bike object to add
     * @return  true if successful, else false
     */
    public boolean add(Bike bike);

    /**
     * Remove a bike object from the list
     * @param bike  the bike object to remove
     * @return  true if successful, else false
     */
    public boolean remove(Bike bike);

    /**
     * Find a bike given its registration number
     * @param regNo  the registration number
     * @return  the bike object, or null if not found
     */
    public Bike find(String regNo);

    /**
     * Sort by registration number
     * @return  the number of comparisons
     */
    public int sortByRegNo();

    /**
     * Sort by registration year
     * @return  the number of comparisons
     */
    public int sortByYear();

    /**
     * Sort by price
     * @return  the number of comparisons
     */
    public int sortByPrice();

    /**
     * Sort by model description
     * @return  the number of comparisons
     */
    public int sortByModel();

    /**
     * Clear the list
     */
    public void clear();

    /**
     * Get the size of the list (= number of bikes)
     * @return  the list size
     */
    public int size();

    /**
     * Convert list to single string with \n after each record
     * @return the string as above
     */
    public String toString();

    /**
     * Get an iterator object to traverse the bike list
     * @return  a java.util.Iterator<Bike> object
     */
    public java.util.Iterator<Bike> iterator();
}

Thank you in advance, sorry for the length, I wasn't sure how much is important.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间你老了 2024-11-03 01:43:18

我认为问题在于:

import java.util.*;

public interface Comparator<T>
{

    int compare(T o1, T o2);
    int getCount();
}

您正在定义自己的比较器接口。相反,您应该摆脱此接口并使用java.util.Comparator

http://download.oracle.com/javase/6/docs/api/java/util/Comparator.html

I think the problem lies in this:

import java.util.*;

public interface Comparator<T>
{

    int compare(T o1, T o2);
    int getCount();
}

you're defining your own Comparator interface. Instead, you should get rid of this interface and use java.util.Comparator

http://download.oracle.com/javase/6/docs/api/java/util/Comparator.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文