• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing arraylist member object methods in enhanced for loop?  RSS feed

 
Ben Allen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried a couple ways to do it, and they don't work. I'm aiming for functionality like I got with the regular for loop, but from an enhanced for loop. Is this simply beyond the scope of an enhanced for loop, or am I just not getting the right syntax?



The TestObject class is simply an int and a String, with getters getInt and getString. It all works fine with the regular for loop.

edit: I should probably mention that I know what I have in the enhanced for loop now will only display the class name and the hash. I've tried adding the .getString and .getInt, and tried a few other ways to make it work. I just reverted to this because it compiles and runs
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried this?


What are you trying to do in enchanced for loop with intTester?
 
Ben Allen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That works, thanks, I didn't realize that intTester was the increment variable, I should have realized it on my own :blush:

Thanks again!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest the problem is in the TestObject class. You haven't overridden toString(),←as that link tells you.
 
Ben Allen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that overriding toString() would let me simply sysout each iteration, but I wanted to access the methods of each member object, not just print the contents of the member variables. The solution above was just what I needed
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should do both: override toString and print the results from the getXXX methods.
 
Ben Allen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why? Best practices, or just for the practice?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both

It is best practice to override toString on just about everything you write. You get good practice by printing out the object and its getXXX methods so you can compare the two.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wait till you see logs like this:
ERROR Cannot connect to com.company.Host@1fc31a
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!