• 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 ArrayList by iterator

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to get the following code to work, but it fails in the last section. When I try to access the list through the iterator it returns an array of Strings, but I'm unable for figure out the syntax to get the elements of the array. I've tried various ways of accessing them and different casts but none have worked. The more I change it, the worse it gets. Maybe the problem is built into the design I used? Any suggestions would be very appreciated.

[Added code tags - see UseCodeTags for details]


Output:
There are 4 element in the list
0) Theodore Seuss Geisel
1) Samuel Langhorne Clemens
2) Hector Hugh Munro
3) Charles Lutwidge Dodgson

[Theodore, Seuss, Geisel]
[Samuel, Langhorne, Clemens]
[Hector, Hugh, Munro]
[Charles, Lutwidge, Dodgson]
 
Ranch Hand
Posts: 199
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well,
you have initialized a list of list and the behavior that you get is the expected.

Try


Best regards,
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Larry Krigbaum wrote:Maybe the problem is built into the design I used?



I'd say that's part of it. The 2D array is icky. It would make more sense to define a Name class with first, middle, and last fields, and an override of the toString() method.

After you get it working that way, it may be worth coming back to this approach to see if you can get it working both for comparison of the approaches and to brush up whatever are of Java is keeping you from getting it working now.
 
Larry Krigbaum
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the suggestions. I'm trying to see if I can implement them. I think the best approach may well be to just implement my own linked list specific to the data I'm working with. I really like the idea of using generic Java collections, but they never seem to be very useful for most applications. Thanks again.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Larry Krigbaum wrote:I think the best approach may well be to just implement my own linked list specific to the data I'm working with.



No, that would be a lot of work for no reason.

I really like the idea of using generic Java collections, but they never seem to be very useful for most applications.



Then you don't yet understand how to use them correctly. They work very well for very, very many situations. Your case here is very simple and straightforward and common. You were given suggestions for two different approaches, either one of which will give you the results you want very simply, without needing to implement your own list. I've been programming with Java full time for 15 years and I have never once needed to implement my own collection.
 
reply
    Bookmark Topic Watch Topic
  • New Topic