• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Define an Iterator for a nested ArrayList  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I try to learn java, so please don't criticize too harshly! :-)

I try to use an Iterator to iterate through an ArrayList of ArrayList<Strings>

Code would like this (inspired from OCA Java SE8 Programmer 1 Certification Guide by Mala Gupta):


//Here I try to define an Iterator for nestedArrayList to be able to also remove elements. That is why I don't want to use the enhanced for loop.
                 Iterator <ArrayList<ArrayList<String>>> it = nestedArrayList.iterator<ArrayList<ArrayList<String>>>();//COMPILATION ERROR
//but Eclipse hits me with a compilation error: Syntax Error on token "(". Expression expected after this token.
//Tried all combinations for the syntax but nothing works. What do I miss?

Thanks, and hope that this forum believes in "There are no stupid questions"...

Kevin
 
Master Rancher
Posts: 2928
102
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Kevin,

welcome to the ranch!

If you have a List<T> myList, then all that is required to get an iterator is:

So, following your example, we may use:

As an aside: watch my use of the diamond operator. It can save quite some typing and was introduced in java 1.7. For example, see this line of code:
 
author
Sheriff
Posts: 23604
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The interator() method of an ArrayList<ArrayList<String>> instance returns an Iterator<ArrayList<String>> instance, and not a Iterator<ArrayList<ArrayList<String>>> instance. You have one level of indirection too much.

And Piet beat me to the answer... have a cow...

Henry
 
Saloon Keeper
Posts: 5009
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kevin Lentner wrote:


Your 'nestedArrayList' is a list of 'ArrayList<String>' not 'ArrayList<ArrayList<String>>'.
 
Kevin Lentner
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prroblem/Quetion resolved!  :-)

Thanks to everyone who answered my question.
I learned from all of you.

Regards,

Kevin,
PS: I there is a better way to say thank you and to mark the question as Resolved please share it with me as I could not find anything in the FAQ.
 
Sheriff
Posts: 12530
206
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kevin Lentner wrote:PS: I there is a better way to say thank you...


This is the best way to do that:

You wrote:Thanks to everyone who answered my question.
I learned from all of you.


ヾ(⌐■_■)ノ♪
 
Marshal
Posts: 60860
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
You cannot use a for‑each loop to remove elements from that List, but you probably can with a for loop. That has the advantage of having the Iterator declared as a loop‑local variable, so it goes out of scope faster.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!