Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Interating through an ArrayOrderedList and modifying elements within

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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:

    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?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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?

    ArrayList<Account> bank = new ArrayList<Account>();
    // ... other code
    // get back the first account
    Account first = bank.get(0);

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: Interating through an ArrayOrderedList and modifying elements within

    Quote Originally Posted by xecure View Post
    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.

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Interating through an ArrayOrderedList and modifying elements within

    Quote Originally Posted by copeg View Post
    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

  8. #8
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Interating through an ArrayOrderedList and modifying elements within

    Quote Originally Posted by javapenguin View Post
    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 View Post
    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 View Post
    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.

Similar Threads

  1. Shuffling elements in a linked list.
    By xecure in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 1st, 2010, 02:25 PM
  2. Transferring elements between arraylists
    By KipTheFury in forum Collections and Generics
    Replies: 6
    Last Post: August 23rd, 2010, 03:13 PM
  3. Help with Arrays - Counting elements
    By ShakeyJakey in forum Collections and Generics
    Replies: 7
    Last Post: August 8th, 2010, 05:09 PM
  4. accessing elements in a 2d ArrayList
    By Flamespewer in forum Collections and Generics
    Replies: 2
    Last Post: March 8th, 2010, 06:11 PM
  5. Method Adding elements to an array with certain restrictions
    By Newoor in forum Collections and Generics
    Replies: 1
    Last Post: December 13th, 2009, 11:13 AM