【问题标题】:Create several new objects within a for-loop in Java在 Java 的 for 循环中创建几个新对象
【发布时间】:2012-05-10 22:04:38
【问题描述】:

我想在 for 循环中从一个类创建多个对象。但我不知道如何编码。我所写的内容创建了一个新对象,但它覆盖了之前的对象。

package assginment1_version4;

import java.util.*;

public class Client {

public static void main (String[] args) {
    System.out.println ("this is a bill database");
    System.out.println ("add a user?(Y/N)");

    Scanner input = new Scanner(System.in);
    String answer = input.nextLine ();
    ArrayList ary = new ArrayList ();

    for (int i=1 ; i < 100; i++) {
        if (answer.equalsIgnoreCase("y")) {
            Bill bill1 = new Bill();
            System.out.println("user first name:");
            bill1.setFname (input.nextLine());
            System.out.println("user Last name:");
            bill1.setLname (input.nextLine());
            System.out.println ("add a user?(Y/N)");
            answer = input.nextLine ();
        } else if (answer.equalsIgnoreCase ("n")) {
            if (Bill.getBillCounter () == 0) {
                System.out.println ("the Database is empty");
                break;
            } else {
                System.out.println ("Number of Users:  "
                        + Bill.getBillCounter ());
                break;
            }
        } else {
            while (!answer.equalsIgnoreCase ("n")
                    && !answer.equalsIgnoreCase ("y")) {
                System.out.println ("add a user?(Y/N)");
                answer = input.nextLine ();
                }
            }
        }
    }
}

请帮我完成这段代码。

【问题讨论】:

  • 你到底想做什么?
  • 我想向这个数据库添加新对象(bill2、bill3、...),但是我的代码将新对象写入了前一个对象。我想将所有对象信息保留在我的数据库中。
  • @msc87 如果您将有助于解决问题的答案标记为已接受的答案(加上您获得 2 业力),这将很有帮助!

标签: java


【解决方案1】:

您正在覆盖它们,因为您在每个循环上创建了一个新的 Bill 并且永远不会将它们保存在任何地方。我相信您想将它们添加到您的ArrayList

首先,您应该在ArrayList 中添加一个类型:

ArrayList<Bill> ary = new ArrayList<Bill>();

然后,在您从用户那里获得关于是否添加新的Bill 的输入之前,您应该将当前的添加到此列表中:

...
System.out.println("user Last name:");
bill1.setLname(input.nextLine());
ary.add(bill1);
...

【讨论】:

  • 按照您的建议进行操作,现在我可以保存新对象,但还有另一个问题....我无法检索对象的信息。例如; System.out.println(bill1.getFname(ary.get(index)))
  • @msc87 请改用ary.get(index).getFname()(虽然我不能确定没有看到 Bill 类信息。
  • 我把 Bill 类放在这里,我按照你说的做了,但它返回了这个错误:线程“main”java.lang.IndexOutOfBoundsException 中的异常:索引:1,大小:1 在 java.util .ArrayList.RangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at assginment1_version2.Client.main(Client.java:46)
  • @msc87 由于这是一个新问题,您应该接受一个答案,然后针对这个新问题提出一个新问题,或者用您的新问题编辑您的原始问题。此外,更新时,不要发布作为答案,您应该编辑您的原始问题。
  • @msc87 数组在 java 中是 0。如果您的数组列表的长度为 1,那么您将通过:ary.get(0).getFname() 获得 Bill
【解决方案2】:

您还没有使用ArrayList,您需要在for循环的末尾添加Bill's对象。

ary.add(bill1);

并向您的 ArrayList 添加一个类型

ArrayList<Bill> ary = new ArrayList<Bill>();

【讨论】:

    【解决方案3】:

    这是比尔类.....

    package assginment1_version2;
    
    public class Bill {
    
    /**
     *  Attributes of a bill
     */
    private String firstName;
    private String lastName;
    private int paymentDeadline;
    private int paymentCode;
    private int billCode;
    
    /**
    * Attribute of Bill Class
    */
    
          private static int BillCounter=0;
    
    /**
     *  Methods of Bill class
     * @return number of users
     */
    /*public static int getBillCounter(){
        return BillCounter;
    }*/
    
    
    /**
     * Class Constructor
     * @param Fname is the first name of user
     * @param Lname is the last name of user
     * @param Pdeadline is the deadline of paying the bill
     * @param Pcode introduces the payment uniquely 
     * @param Bcode introduces the bill uniquely
     */
        public Bill (){
            BillCounter++;
        }
    
    /**
     *  FirstName methods 
     *  method to set FirstName
     * @param n is the input of setname method as a user name
     */
    public void setFname (String n){
        firstName=n;
    }
    // method to get FirstName
    public String getFname (){
        return firstName;
    }
    
    
    /**
     *  LastName methods
     *  method to set LastName
     */
    public void setLname (String m){
        lastName=m;
    }
    // method to get LastName 
    public String getLname(){
        return lastName;
    }
    
    
    /**
     * PaymentDeadline methods
     * method to set PaymentDeadline     
     */
    public void setPaymentDeadline(int m){
        paymentDeadline= m;
    }
    //method to get PaymentDeadline
    public int getPaymentDeadline(){
        return paymentDeadline;
    }
    
    /*
     * PaymentCode methods
     * Method to set PaymentCode
     */
    public void setPaymentCode (int m){
        paymentCode=m;
    }
    //method to get PaymentCode
    public int getPaymentCode(){
        return paymentCode;
    }
    
    /*
     * Methods of BillCode
     * method to set BillCode 
     */
    public void setBcode(int Bcode){
        billCode=Bcode;
    }
    //method to get BillCode
    public int getBcode(){
        return billCode;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      相关资源
      最近更新 更多