Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

myArrayList.get()

  Asked By: Hamish    Date: Jul 23    Category: Java    Views: 447
  

I have a dynamic ArrayList that I am using to populate a bank with
bank accounts. When I open a new account I add the account to the
ArrayList. Later I want to print what I have just done by showing the
account number and the initial deposit. The problem is I don't know
how to call the indexed account with an integer. The accounts are
added in this form at the end 0,1,2,3, etc. The only thing I could
think of was myArrayList.get - but what is the parameter of get if
the ArrayList is dynamic and automatically handles the numbering of
the index. Here is the method. If anyone knows what instance method
to place in the println statement please reply:

private void openNewAccount()
{

// prompt for initial deposit
int startup = atm.readInt( "Initial deposit: " );

// create newAccount
BankAccount newAccount = new BankAccount( startup, this );

// @@@DeMarco add it to accountList
accountList.add(newAccount);

// @@@DeMarco inform user without including account number
atm.println( "opened new account " + ??? place account number
here ??? + " with $" + newAccount.getBalance());

}

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Josie Roberts     Answered On: Jul 23

Does your BankAccount class have get method  to access the
Account Number? You can use that in the println.

private void  openNewAccount()
{

// prompt  for initial deposit
int startup  = atm.readInt( "Initial deposit: " );

// create  newAccount
BankAccount newAccount = new BankAccount( startup, this );

// @@@DeMarco add  it to accountList
accountList.add(newAccount);

// @@@DeMarco inform user  without including account number
atm.println( "opened new account " + newAccount.getAccountNumber()
+ " with $" + newAccount.getBalance());

}

 
Answer #2    Answered By: Marc Anderson     Answered On: Jul 23

That's the thing. The last version of the BankAccount class has no
AccountNumber member, which makes it very hard to uniquely identify
each account.

If you can't alter the BankAccount class, then the easiest way to keep
track of where the accounts are in the list is to keep a second list
of the account numbers in the same order as the accountList. Then,
when you need to find an account in the list, you can search the
number list to get the index  you need.

Of course, adding the account number  to the BankAccount class is a
better fix.

 
Answer #3    Answered By: Kiet Jainukul     Answered On: Jul 23

No I wanted to use a method  from ArrayList and I am now using this:

accountList.lastIndexOf(newAccount)

because the accounts are added to the ArrayList dynamically at the
end of the list. If your interested in the code find it here:

http://www.reynolddemarcojr.com/Bank.java
http://www.reynolddemarcojr.com/BankAccount.java
http://www.reynolddemarcojr.com/Terminal.java

 
Didn't find what you were looking for? Find more on myArrayList.get() Or get search suggestion and latest updates.




Tagged: