包含 ArrayList 的对象

huangapple go评论83阅读模式
英文:

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 = &quot;Name&quot;;
        int gameP = 10;
        String gameR = &quot;Review&quot;;
        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&lt;String&gt; gameReviews;

    public Game(String gameName, int gamePrice, ArrayList&lt;String&gt; gameReviews) {
        this.gameName = gameName;
        this.gamePrice = gamePrice;
        this.gameReviews = gameReviews;
    }
    public static void main(String[] args) {
        String gameN = &quot;Name&quot;;
        int gameP = 10;
        ArrayList&lt;String&gt; gameReviews =  new ArrayList&lt;String&gt;();
        
        gameReviews.add(&quot;Review 1&quot;);
        gameReviews.add(&quot;Review 2&quot;);
        gameReviews.add(&quot;Review 3&quot;);
        gameReviews.add(&quot;Review 4&quot;);
        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&lt;String&gt;

public class Game {
    private String gameName;
    private int gamePrice;
    private List&lt;String&gt; gameReviews;

    public Game(String gameName, int gamePrice, List&lt;String&gt; 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&lt;&gt;();
    }

    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&lt;String&gt; reviews = new ArrayList&lt;&gt;();
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(&quot;This is a review of Game ...&quot;);

huangapple
  • 本文由 发表于 2020年10月23日 23:23:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64502882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定