错误消息-Game cannot be converted to GameCollection

我想将二进制搜索算法应用于名为gameCollection的数组。该数组使用Game类中的一组变量来定义数组的组件。我将记录集添加到数组,但是算法不想找到中间值。

我认为这是因为集合不是按字母顺序排列的,这可能会导致错误,这是另一个问题,这是如何使用其他类对数组进行排序?当我做一些研究时,我仍然不确定。到目前为止,这是我的代码:

GameCollection类:

public class GameCollection  {
    // instance variables - replace the example below with your own
    private ArrayList<Game> gameCollection = new ArrayList<Game>();

    /**
     * Constructor for objects of class GameCollection
     */
    public GameCollection()
{
    setGameCollection();
}

public void setGameCollection()
{
    Game g1 = new Game("Little Big Planet", "PS3", 34, 7, 2014, "");
    Game g2 = new Game("Grand Theft Auto 4", "PS3", 23, 3, 2013, "");
    Game g3 = new Game("Grand Theft Auto 4", "X360", 23, 12, 2012, "");
    Game g4 = new Game("Last Of Us", "PS3", 23, 18, 2011, "");
    Game g5 = new Game("Metal Gear Rising: Revengeance", "Win", 76, 7, 2010, "");
    Game g6 = new Game("Don't Starve: Console Edition", "PS4", 5, 3, 2014, "");
    Game g7 = new Game("Grand Theft Auto 5", "PS4", 34, 12, 2008, "");
    Game g8 = new Game("KickBeat", "Win", 23, 18, 2007, "");
    Game g9 = new Game("Tomb Raider: Definitive Edition", "PS4", 87, 15, 2006, "");
    Game g10 = new Game("Tomb Raider: Definitive Edition", "XBO", 2, 18, 2005, "");
    Game g11 = new Game("NaissanceE", "Win", 34, 7, 2014, "");
    Game g12 = new Game("LocoCycle", "X360", 4, 3, 2013, "");
    Game g13 = new Game("LocoCycle", "Win", 56, 12, 2012, "");
    Game g14 = new Game("Assassin's Creed IV: Black Flag - Freedom Cry", "PS3", 10, 18, 2011, "");
    Game g15 = new Game("Assassin's Creed IV: Black Flag - Freedom Cry", "PS4", 11, 15, 2010, "");
    Game g16 = new Game("Basement Crawl", "PS4", 34, 3, 2009, "");
    Game g17 = new Game("Castlevania: Lords of Shadow 2", "PS3", 22, 12, 2008, "");
    Game g18 = new Game("Castlevania: Lords of Shadow 2", "X360", 46, 18, 2007, "");
    Game g19 = new Game("Castlevania: Lords of Shadow 2", "Win", 55, 15, 2006, "");
    Game g20 = new Game("Dead Nation: Apocalypse Edition", "PS4", 43, 3, 2005, "");
    Game g21 = new Game("South Park: The Stick of Truth", "PS3", 52, 12, 2004, "");

    gameCollection.add(g1);
    gameCollection.add(g2);
    gameCollection.add(g3);
    gameCollection.add(g4);
    gameCollection.add(g5);
    gameCollection.add(g6);
    gameCollection.add(g7);
    gameCollection.add(g8);
    gameCollection.add(g9);
    gameCollection.add(g10);
    gameCollection.add(g11);
    gameCollection.add(g12);
    gameCollection.add(g13);
    gameCollection.add(g14);
    gameCollection.add(g15);
    gameCollection.add(g16);
    gameCollection.add(g17);
    gameCollection.add(g18);
    gameCollection.add(g19);
    gameCollection.add(g20);
    gameCollection.add(g21);

    Collections.sort(gameCollection, new Comparator<Game>()
    {
        @Override
        public int compare(Game g1, Game g2)
        {
            return g1.title.compareTo(g2.title);
        }
    }
    );

    //ArrayList.sort(gameCollection);
    //this collection must be in alfabetical order
}

public GameCollection searchGame(String title)
{
    if (gameCollection.size() == 0)
    {
        return null;
    }

    int low = 0;
    int high = gameCollection.size()-1;

    while(low <= high)
    {
        int middle = (low + high) / 2;
        if (title.compareTo(gameCollection.get(middle).getTitle()) > 0)
        {
            low = middle + 1;
        }
        else if (title.compareTo(gameCollection.get(middle).getTitle()) <0)
        {
            high = middle - 1;
        }
        else
        {
            return gameCollection.get(middle);
        }
    }
}
}


这是我的游戏课:

public class Game
{
// instance variables - replace the example below with your own
public String title;
public String console;
public int quantity;
public int ageRating;
public int releaseDate;
public String pict;

/**
 * Constructor for objects of class Game
 */
public Game(String title, String console, int quantity, int ageRating, int releaseDate, String pict)
{
    // initialise instance variables
    this.title = title;
    this.console = console;
    this.quantity = quantity;
    this.ageRating = ageRating;
    this.releaseDate = releaseDate;
    this.pict = pict;
}

public void setTitle(String title)
{
    this.title = title;
}

public void setConsole(String console)
{
    this.console = console;
}

public void setQuantity(int quantity)
{
    this.quantity = quantity;
}

public void setAgeRating(int ageRating)
{
    this.ageRating = ageRating;
}

public void setReleaseDate(int releaseDate)
{
    this.releaseDate = releaseDate;
}

public void setPict(String pict)
{
    this.pict = pict;
}

/**
 * An example of a method - replace this comment with your own
 *
 * @param  y   a sample parameter for a method
 * @return     the sum of x and y
 */
public String getTitle()
{
    return title;
}

public String getConsole()
{
    return console;
}

public int getQuantity()
{
    return quantity;
}

public int getAgeRating()
{
    return ageRating;
}

public int getReleaseDate()
{
    return releaseDate;
}

public String getPict()
{
    return pict;
}

public String toString()
{
    //return all the variables (contcatination)
    return "<html>Game Details:<br>"
    + "<br>Title:" + title
    + "<br>Console:" + console
    + "<br>Quantity:" + quantity
    + "<br>Age Rating:" + ageRating
    + "<br>Release Date:" + releaseDate
    + "<br>Picture:" + pict;
}

}

最佳答案

正如我所评论的,searchGame签名返回GameCollection,但是您的return子句返回Game,因为gameCollectionGame的数组。

因此,有两种可能性,具体取决于您的需求:


将方法的签名更改为:public Game searchGame(String title)
更改return子句以返回包含所需元素的gameCollection子列表:gameCollection.subList(middle, middle+1);

08-07 02:17