Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何创建一个对象数组,在调用时创建新帐户?_Java_Arrays_Object_Bank - Fatal编程技术网

Java 如何创建一个对象数组,在调用时创建新帐户?

Java 如何创建一个对象数组,在调用时创建新帐户?,java,arrays,object,bank,Java,Arrays,Object,Bank,我需要编程作业方面的帮助。BankAccount类已存在,并正常工作。以前从未有人要求我将对象放入数组,我在这样做时遇到了困难 我从以下几点开始: public class Bank { private BankAccount[] Bank; public Bank(BankAccount[] Bank) { BankAccount[] b1 = new BankAccount[10]; } 虽然它是编译的,但它是错误的。我不知道去哪里 以下是

我需要编程作业方面的帮助。BankAccount类已存在,并正常工作。以前从未有人要求我将对象放入数组,我在这样做时遇到了困难

我从以下几点开始:

   public class Bank
   {
   private BankAccount[] Bank;

   public Bank(BankAccount[] Bank)
   {
      BankAccount[] b1 = new BankAccount[10];
   }
虽然它是编译的,但它是错误的。我不知道去哪里

以下是我目前一直坚持的代码要求

Bank类的对象最多可以容纳10个BankAccount对象。 Bank对象的构造函数将创建一个最多可容纳10个BankAccount对象的数组。 以下代码是我们的教授在我们必须使用的作业中包含的测试程序:

System.out.println("\nCreate bank1");
Bank bank1 = new Bank();
System.out.println("\nOne account");
BankAccount b1 = new BankAccount("Joe Mac", 1234);
b1.adjust(1000.0);
bank1.addAccount(b1);
bank1.printAccounts();
System.out.println("\nTwo accounts");
BankAccount b2 = new BankAccount("Sally Ride", 2345);
b2.adjust(2000.0);
bank1.addAccount(b2);
bank1.printAccounts();
System.out.println("\nThree accounts");
BankAccount b3 = new BankAccount("Pat Armstrong", 3456);
b3.adjust(3000.0);
bank1.addAccount(b3);
bank1.printAccounts();
System.out.println("\nMonthly Fee");
bank1.monthlyFee(fee);
bank1.printAccounts();
System.out.println("\nErrors:");

非常感谢您的帮助。谢谢。

您只需添加一个方法来添加一个新帐户,并测试它不会超过10:

 public class Bank
   {
   private BankAccount[] accounts; //Don't name variables with uppercase
   private int accountsPointer; //This is going to keep track of how many accounts are there in the array

   public Bank() //The constructor doesn't need to accept any bank account since it'll start as empty
   {
    accounts = new BankAccount[10]; //Here we initialize the array
    accountsPointer = 0; //Here the pointer starts as 0 since the array is empty
   }
   public void addAccount(BankAccount account){
       accountsPointer++;
       if(accountsPointer<10){ //We test it here so it won't throw an out of bounds exeption
           accounts[accountsPointer-1]=account;//It assigns the account to the next empty space on the array
       }
   }

您可以使用以下代码来解决您的问题

class Bank {
    private static final int MAX_NO_OF_ACCOUNT = 10;
    private int pointer = 0;
    private BankAccount[] bank;

    public Bank() {
        bank = new BankAccount[MAX_NO_OF_ACCOUNT];
    }

    public void addAccount(BankAccount bankAccount) throws Exception {
        if (pointer == MAX_NO_OF_ACCOUNT) {
            throw new Exception("No of bank account reached max no"); // you should own custom exception
        } else {
            bank[pointer] = bankAccount;
            pointer++;
        }
    }

}
说明: 根据要求,一个银行对象最多可持有10个账户。我已经把它放在可用的名称MAX_NO_OF_ACCOUNT中,因为我知道它是常量。Java中的静态最终变量称为常量。此外,它还可以灵活地更改帐户的最大数量,因为这可能会在多个位置使用。我必须将其私有化,因为我不想在类之外公开该值。但是,我认为应该更多地由配置驱动。指针变量将始终指向最后添加的元素索引,以便在添加之前进行检查。让我们来看看构造器。当我创建一个银行对象时,我已经初始化了BankAccount[]数组,该数组可以保存帐户的最大帐户号

现在添加帐户方法:
我将addAccount方法作为选中的异常,因为我认为该API/类的用户可以添加超过最大数量的account。如果发生这种情况,我们将强制处理这种情况

你的问题是什么?将addAccountBankAccount方法添加到你的Bank类并在那里执行数组操作。如何创建对象数组,就像我在这里尝试创建的那样。我不知道如何命名。不要用大写字母命名变量,这很容易混淆。不要为BankAccounts银行命名变量,这非常容易混淆,尤其是在名为Bank的类中。数组在许多部分的行为与其他类型相同,因此如果在构造函数中声明b1,则其可见性仅限于构造函数。你写过没有数组成员的类吗?谢谢你的回答!这看起来很有帮助,但它将受益于对代码的作用以及如何解决OPs问题的一些解释,以及它与第一个答案的区别。请参考我对第一个答案的评论。除此之外,第一个答案已经改进,而您的答案仍然没有任何解释。我已经添加了解释。第一个答案在逻辑上仍然存在重大问题。这并不能解决问题。好的,Lengh=10将始终为false。因此它不会在if条件下进入。您的代码也将无法编译,因为数组上没有可用的大小。@AmitBera您是对的,我没有意识到,由于数组已经初始化,它将始终是10,谢谢。不过,还是有一些错误。当这个accounts[accounts.size]=account时会发生什么;行执行。这与要求不符,而且它会抛出ArrayIndexOutOfBoundsException。@AmitBera你又对了,我忘记更正了,再次感谢。仍然存在一些问题。将第一个帐户添加到银行对象时会发生什么?它将尝试将帐户添加到数组的-1索引中。所以,accounts[accountsPointer-1]=account应该是accounts[accountsPointer]=account;