This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Accessing ArrayList by iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Accessing ArrayList by iterator" Watch "Accessing ArrayList by iterator" New topic
Author

Accessing ArrayList by iterator

Larry Krigbaum
Greenhorn

Joined: Oct 25, 2002
Posts: 8
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]
Carles Gasques
Ranch Hand

Joined: Apr 19, 2013
Posts: 199
    
    1
Well,
you have initialized a list of list and the behavior that you get is the expected.

Try


Best regards,
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 25, 2002
Posts: 8
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing ArrayList by iterator
 
Similar Threads
accessing a while loop variable outside the loop
Problem While Removing Element From List
Extracting hierarchy of objects from Vector
Getting Objects from Collections
Conversion of Raw Soap Response Into HTML