• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why can't i use Iterator on a String array?  RSS feed

 
narendra nath
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have an array of String variables.

How come I can't use Iterator on the String array? It is giving compiling error.

Tahnks much
nath

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because arrays don't have iterators.

The .iterator() method is on the Collections API, of which arrays are not.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basam nath wrote:
How come I can't use Iterator on the String array? It is giving compiling error.


Arrays don't have iterators. It is accessed via an index to the element that you want. Furthermore, you can also simply "iterate" through an array via the for-each loop.

Henry
 
narendra nath
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thought so.

But I wanted to hear from professional java developers.



thanks much
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays have 11 methods (10 inherited from the Object class and 1 overridden from the Object class) 1 (public final) field (=length) and that is it. They are also implicitly Cloneable and Serializable. It says so in the Jave® Language Specification, so woe betide anybody who tries to implement Java® arrays with more members
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!