Interating through an ArrayOrderedList and modifying elements within
I has having problem iterating through an ArrayOrderedList and modifying elements in them. I am trying to write a bank software. I have a Bank class which creates an ArrayOrderedList. Within each element in the ArrayOrderedList is stored an Account object (either CheckingAccount or SavingsAccount). I have accessor and mutator methods for each of those classes. What I am trying to do is update the balance of an account and this is how I am trying to do it:
Code :
01 public static void deposit(String acctnum, double amt)
02 {
03 Account account = new CheckingAccount(new Customer(), 0, "acctnum");
04 int i = Bank.find(account);
05
06 if(i > 0)
07 {
08 Bank[i].deposit(amt);
09 }
10 }
I get an error on Bank[i].deposit(amt);
Eclipse is telling me it is a syntax error which says "type of expression must be an array but it resolved to ArrayOrderedList<Account>"
Not sure what is going on there, can someone help me?
Re: Interating through an ArrayOrderedList and modifying elements within
Java doesn't provide operator overloading, so you must use getter methods to access the data inside of the ArrayOrderedList. I'm not familiar with the ArrayOrderedList class, though. Did you mean an ArrayList?
Code Java:
ArrayList<Account> bank = new ArrayList<Account>();
// ... other code
// get back the first account
Account first = bank.get(0);
Re: Interating through an ArrayOrderedList and modifying elements within
Sorry I forgot to mention that. ArrayOrderedList is a subclass of ArrayList. I'm using programmer defined ArrayOrderedList and ArrayList class. No get method is defined.
Re: Interating through an ArrayOrderedList and modifying elements within
Generally, it's not a good idea to subclass an ArrayList. Arrays (and in fact all lists) should by default be ordered (not necessarily in the order you want, but they are order non-the-less).
If you have no get method (or some other similar method), then you cannot retrieve elements from your ArrayOrderedList.
Re: Interating through an ArrayOrderedList and modifying elements within
Is an OrderedArrayList an ArrayList that implements comparable and can be in order, like numeric or alphabetic order? I've been wondering if there was such a class.
Re: Interating through an ArrayOrderedList and modifying elements within
Quote:
Originally Posted by
xecure
ArrayOrderedList is a subclass of ArrayList...No get method is defined.
If ArrayOrderedList is a subclass of ArrayList, get is defined in the parent class ArrayList (technically defined in the List interface). Second helloworlds advice regarding extending ArrayList...not sure why you want to do this.
Re: Interating through an ArrayOrderedList and modifying elements within
Quote:
Originally Posted by
copeg
If ArrayOrderedList is a subclass of ArrayList, get is defined in the parent class ArrayList (technically defined in the List interface).
oh yeah, duh. I forgot about the whole inheritance thingy :P
Re: Interating through an ArrayOrderedList and modifying elements within
Quote:
Originally Posted by
javapenguin
Is an OrderedArrayList an ArrayList that implements comparable and can be in order, like numeric or alphabetic order? I've been wondering if there was such a class.
Yes, my ArrayOrderedList uses the comparable interface to add item in the array according to their ID's (account number int his case)
Quote:
Originally Posted by
helloworld922
Generally, it's not a good idea to subclass an ArrayList. Arrays (and in fact all lists) should by default be ordered (not necessarily in the order you want, but they are order non-the-less).
If you have no get method (or some other similar method), then you cannot retrieve elements from your ArrayOrderedList.
I see, but for my assignment I must do this, as per instruction from my professor
Quote:
Originally Posted by
copeg
If ArrayOrderedList is a subclass of ArrayList, get is defined in the parent class ArrayList (technically defined in the List interface). Second helloworlds advice regarding extending ArrayList...not sure why you want to do this.
In my programmer defined ArrayOrderedList and ArrayList no get is defined, that is why I was having these problems. I will try and implement a get method now.