类无法转换为ArrayList(Java)

时间:2015-05-04 15:19:21

标签: java arrays arraylist bluej

错误消息 - file_get_contents('http://google.com');

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

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

GameCollection类:

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);
        }
    }
}
}

1 个答案:

答案 0 :(得分:1)

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

因此,根据您的需要,有两种可能性:

  • 将方法的签名更改为:public Game searchGame(String title)

  • 更改return子句以返回包含所需元素的gameCollection子列表:gameCollection.subList(middle, middle+1);