【问题标题】:Multiple object in array (java)数组中的多个对象(java)
【发布时间】:2015-02-13 03:24:07
【问题描述】:

我想使用Scanner 的结果创建对象并将它们添加到数组中。

但是,每次我第二次要求用户输入时,它只会覆盖第一个对象。

如何将多个对象添加到数组中?

这是我的代码:

public void ajoutadd() {
    int i=0;
    boolean boucle=true;
    while(i!=2){
        Scanner thegame = new Scanner(System.in);
        System.out.print("name: \n");
        String jname = lejeu.nextLine();
        System.out.print(jname);
        Scanner qty = new Scanner(System.in);
        System.out.print("qty \n");
        int jqty = qty.nextInt();
        Scanner cat = new Scanner(System.in);
        System.out.print("cat: \n");
        String categ = cat.nextLine();
        Scanner price = new Scanner(System.in);
        System.out.print("price: \n");
        int jprice = price.nextInt();
        Game agame = new Game(jname,jqty,categ,jprice);
        System.out.print(unjeu.Nom);

        // creating the array to contain the game(s)
        ArrayList<Game> thegame = new ArrayList<Game>(); 

        thegame.add(new Game(jname,jqty,categ,jprice));

        // actually only display 1 object that is overwritten
        // each time after the loop
        System.out.println(thegame);

        i=i++;
    }
}

【问题讨论】:

    标签: java arrays object methods java.util.scanner


    【解决方案1】:

    您的循环实际上并没有覆盖第一个值。每次循环时它只是创建一个new ArrayList

    您应该创建一个 ArrayList 并不断添加。

    例如

    ArrayList<Game> games = new ArrayList<Game>();
    while(...) {
       ...
       games.add(...);
       ...
    }
    

    情侣笔记:

    • 您不必为每个scan 创建一个new Scanner,您可以使用相同的(也可以像数组列表一样在循环外初始化它)。

    • i=i++ 可能只是i++i++ 增加变量 i,它与 i + 1 不同。相当于i = i + 1

    • ArrayList 变量使用List 接口是一种很好的做法,这样无论您使用哪种List 实现,您的代码都可以正常工作。

    【讨论】:

    • 非常感谢。好的建议和答案。我只创建了一个扫描仪,就像你说的那样,但是 cat 和 price 输入有问题,当编译器到达这些时,它会打印 cat: and price:就像价格:超过猫:没有时间输入猫字符串?不知道你明白我的意思。就像我不能输入猫一样,它会立即跳转到价格?
    【解决方案2】:

    在循环外创建一个Scanner 和一个List 实例(我建议你使用接口)。喜欢,

    public void ajoutadd() {
        int i = 0;
        boolean boucle = true;
        Scanner thegame = new Scanner(System.in);
        List<Game> thegame = new ArrayList<>();
        while (i != 2) {
            System.out.print("name: \n");
            String jname = thegame.nextLine();
            System.out.print(jname);
            System.out.print("qty \n");
            int jqty = thegame.nextInt();
            System.out.print("cat: \n");
            String categ = thegame.nextLine();
            System.out.print("price: \n");
            int jprice = thegame.nextInt();
            Game agame = new Game(jname, jqty, categ, jprice);
            System.out.print(unjeu.Nom);
            thegame.add(new Game(jname, jqty, categ, jprice));
            System.out.println(thegame);
            i++; // <-- not i = i++;
        }
    }
    

    【讨论】:

    • 我如何返回 arraylist 以便能够在其他类中使用它?
    • public void ajoutadd() { 更改为public List&lt;Game&gt; ajoutadd() { 并在while 循环之后添加return thegame;。此外,您可能应该自己提出一个新问题(并进行一些研究)。
    【解决方案3】:

    将列表的初始化移到循环外。目前,您每次都在循环中创建列表对象。因此失去了对现有的参考。

    public  ArrayList<Game> ajoutadd() {
        int i=0;
        boolean boucle=true;
         ArrayList<Game> thegame = new ArrayList<Game>(); 
        while(i!=2){
        Scanner thegame = new Scanner(System.in);
        System.out.print("name: \n");
        String jname = lejeu.nextLine();
        System.out.print(jname);
        Scanner qty = new Scanner(System.in);
        System.out.print("qty \n");
        int jqty = qty.nextInt();
        Scanner cat = new Scanner(System.in);
        System.out.print("cat: \n");
        String categ = cat.nextLine();
        Scanner price = new Scanner(System.in);
        System.out.print("price: \n");
        int jprice = price.nextInt();
        Game agame = new Game(jname,jqty,categ,jprice);
        System.out.print(unjeu.Nom);
       //creating the array to contain the game(s)
        thegame.add(new Game(jname,jqty,categ,jprice));
       each time after the loop
    
    
        i++;
    
        }
      System.out.println(thegame); 
      return  thegame;
    
    
    }
    

    【讨论】:

    • 我如何返回 arraylist 以便能够在其他类中使用它?
    • 将返回类型更改为Arraylist并返回列表。现在你在调用方法中得到这个值。如果对您有用,请接受答案或投票。
    • 我也按照你说的做了,但是当我尝试用其他方法调用它时,比如 public void disp(){ System.out.print(thegame);} 游戏没有被识别。 ..?
    • 从其他方法调用时,将值保存在其他变量中并打印出来。 ArrayList 游戏 = ajoutadd(); System.out.println(游戏);.游戏具有方法级别范围,因此不会在方法之外被识别,但您可以在变量中获取返回值
    • 这是因为这个变量在方法的范围内,但是我们添加了return语句,以便我们在调用方法时获得值。 disp(){ ArrayList 游戏 = ajoutadd() ; System.out.println(游戏); } .
    猜你喜欢
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2013-01-27
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多