• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing an object from an ArrayList of objects

 
Ranch Hand
Posts: 53
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

I have an ATM class, that simulates an ATM. When this class is created in my App, it populates an arrayList with a set of account objects

Im trying to access an account object within this arrayList and return the balance of that account

Here is my ATM class



and here is my account class



Im able to access the slot in the array that has an Account, but how do I access the methods of that particular account object
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't like arithmetic with Math.random(). It is potentially error‑prone. I think it is better to use the methods of a Random object.
balance = myRandom.nextInt(4991) + 10;

Work out which pair of () in your code can be safely dispensed with.
When you access an element in a List, you convert the call to something returning that element. Avoid the get() method on linked lists, but it works well on array lists:-
Account account = listOfAccounts.get(6);
That will of course get you the 7th element. Now you can call methods on it. There are other ways to do that.
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic