英文:
Object that contains ArrayList
问题
以下是翻译好的内容:
我正在编写一个销售游戏的程序。现在在我的代码中,它只会显示客户编写的最后一条评论,但我希望它包含游戏收到的所有评论的列表。我在考虑是否可能将一个 ArrayList
作为变量,而不是 String
类型的评论。以下是当前的代码示例:
public class Game {
private String gameName;
private int gamePrice;
private String gameReviews;
public Game(String gameName, int gamePrice, String gameReviews) {
this.gameName = gameName;
this.gamePrice = gamePrice;
this.gameReviews = gameReviews;
}
}
然后,如果我想创建一个对象,用来替换之前的旧对象,它会是什么样子呢?
public class Games {
public static void main(String[] args) {
String gameN = "Name";
int gameP = 10;
String gameR = "Review";
Game game = new Game(gameN, gameP, gameR);
}
}
英文:
I am doing a program to sell games. Now in my code it will show only the last review the customer writes, but instead I would like it to contain a list of all reviews the game got. I was thinking if it would be possible to have an ArrayList
as a variable instead of the String
review. Here is an example of how it looks like now.
<!-- language: lang-js -->
public class Game {
private String gameName;
private int gamePrice;
private String gameReviews;
public Game(String gameName, int gamePrice, String gameReviews) {
this.gameName = gameName;
this.gamePrice = gamePrice;
this.gameReviews = gameReviews;
}
}
and then if I want to create an object how it would look like to replace the old one I had?
<!-- language: lang-js -->
public class Games {
public static void main(String[] args) {
String gameN = "Name";
int gameP = 10;
String gameR = "Review";
Game game = new Game(gameN, gameP,gameR);
}
}
答案1
得分: 1
可以使用 ArrayList 替代 String。
import java.util.ArrayList;
class Game {
private String gameName;
private int gamePrice;
private ArrayList<String> gameReviews;
public Game(String gameName, int gamePrice, ArrayList<String> gameReviews) {
this.gameName = gameName;
this.gamePrice = gamePrice;
this.gameReviews = gameReviews;
}
public static void main(String[] args) {
String gameN = "Name";
int gameP = 10;
ArrayList<String> gameReviews = new ArrayList<String>();
gameReviews.add("Review 1");
gameReviews.add("Review 2");
gameReviews.add("Review 3");
gameReviews.add("Review 4");
Game game = new Game(gameN, gameP, gameReviews);
}
}
英文:
It is possible to use ArrayList instead of String.
import java.util.ArrayList;
class Game {
private String gameName;
private int gamePrice;
private ArrayList<String> gameReviews;
public Game(String gameName, int gamePrice, ArrayList<String> gameReviews) {
this.gameName = gameName;
this.gamePrice = gamePrice;
this.gameReviews = gameReviews;
}
public static void main(String[] args) {
String gameN = "Name";
int gameP = 10;
ArrayList<String> gameReviews = new ArrayList<String>();
gameReviews.add("Review 1");
gameReviews.add("Review 2");
gameReviews.add("Review 3");
gameReviews.add("Review 4");
Game game = new Game(gameN, gameP,gameReviews);
}
}
答案2
得分: 1
如果您的游戏与评论之间的关系是一对多的,您的评论数据类型可以是 List<String>
public class Game {
private String gameName;
private int gamePrice;
private List<String> gameReviews;
public Game(String gameName, int gamePrice, List<String> gameReviews) {
this.gameName = gameName;
this.gamePrice = gamePrice;
this.gameReviews = gameReviews;
}
public Game(String gameName, int gamePrice) {
this.gameName = gameName;
this.gamePrice = gamePrice;
this.gameReviews = new ArrayList<>();
}
public void addGameReview(String gameReview) {
this.gameReviews.add(gameReview);
}
}
如果您在初始化时有评论列表,只需将其传递给构造函数:
List<String> reviews = new ArrayList<>();
Game game = new Game(gameN, gameP, reviews);
否则,您可以拥有一个构造函数,将 gameReviews 初始化为空的 ArrayList
,并在初始化后添加评论:
Game game = new Game(gameN, gameP);
game.addGameReview("This is a review of Game ...");
英文:
If the relationship between your game and the reviews is one-to-many, the data type of your reviews can be List<String>
public class Game {
private String gameName;
private int gamePrice;
private List<String> gameReviews;
public Game(String gameName, int gamePrice, List<String> gameReviews) {
this.gameName = gameName;
this.gamePrice = gamePrice;
this.gameReviews = gameReviews;
}
public Game(String gameName, int gamePrice) {
this.gameName = gameName;
this.gamePrice = gamePrice;
this.gameReviews = new ArrayList<>();
}
public void addGameReview (String gameReview) {
this.gameReviews.add(gameReview);
}
}
Then if you have the list of reviews at initialization you'd just pass it into the constructor:
List<String> reviews = new ArrayList<>();
Game game = new Game(gameN, gameP, reviews);
Otherwise, you could have a constructor that initializes gameReviews to an empty ArrayList
, and add the review after the initialization
Game game = new Game(gameN, gameP);
game.addGameReview("This is a review of Game ...");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论