gpt4 book ai didi

java - 类无法转换为 ArrayList (Java)

转载 作者:行者123 更新时间:2023-12-01 11:35:30 25 4
gpt4 key购买 nike

错误消息 - 游戏无法转换为 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,因为 gameCollection 是一个 Game 数组。

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

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

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

关于java - 类无法转换为 ArrayList (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30033731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com