• Post Reply Bookmark Topic Watch Topic
  • New Topic

Define an Iterator for a nested ArrayList  RSS feed

 
Kevin Lentner
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
 
Piet Souris
Master Rancher
Posts: 2044
75
  • 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:
 
Henry Wong
author
Sheriff
Posts: 23295
125
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
 
Carey Brown
Saloon Keeper
Posts: 3314
46
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.
 
Junilu Lacar
Sheriff
Posts: 11481
180
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.

ヾ(⌐■_■)ノ♪
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!