• Post Reply Bookmark Topic Watch Topic
  • New Topic

why to use Iterators in java?  RSS feed

 
Ranch Hand
Posts: 1085
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hiii, i am was just reading Head First Design Pattern..
i have a question about iterator pattern.

two loops are there:

for(int i=0; i<ArrayList.size();i++)
Class1 c1 = (Class1)Items1.get();


for(int i=0;i<Array.length;i++)
Class2 c2 = Items[i];

why do we use iterators here??
because of, iterators add one layer while we get data from our ArrayList or Array respectively, so it prevents from expose our data, isn't it??
i also want to know in terms of design patterns...
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Jain wrote:why do we use iterators here??

Well, as far as I can see from your code: you don't.

However, as a general question there are many answers, just a couple of which are:
  • They provide the framework for the 'foreach' loop.
  • They are generally MUCH faster for a structure (such as a LinkedList) that provides fast access between elements, but poor performance for random reads (eg, List.get(int)).

  • HIH

    Winston
     
    Punit Jain
    Ranch Hand
    Posts: 1085
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    so it is not prevent data from exposing??
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Punit Jain wrote:so it is not prevent data from exposing??

    No.

    Winston
     
    Bartender
    Posts: 4568
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Punit Jain wrote:so it is not prevent data from exposing??

    No. It's about having a consistent way of iterating through different types of data structure.
     
    Punit Jain
    Ranch Hand
    Posts: 1085
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    No. It's about having a consistent way of iterating through different types of data structure.


    okay so you mean, suppose i have array and arraylist (or any other collection), although collection already has Iterator Interface, so i don't need to create it for collection, i only need to implement that with collection.
    and for array, i can create an Iterator interface with hasNext(), next(), remove() etc methods, and then implement this with Array, so now both implement Iterator, one is inbuilt, other is which i create.
    so client will be unknown, to array or arrayList, for client both will be same..
    isn't it??
     
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Punit Jain wrote:

    No. It's about having a consistent way of iterating through different types of data structure.


    okay so you mean, suppose i have array and arraylist (or any other collection), although collection already has Iterator Interface, so i don't need to create it for collection, i only need to implement that with collection.
    and for array, i can create an Iterator interface with hasNext(), next(), remove() etc methods, and then implement this with Array, so now both implement Iterator, one is inbuilt, other is which i create.
    so client will be unknown, to array or arrayList, for client both will be same..
    isn't it??


    Yes and no.

    Array classes are generated on the fly by the compiler and/or JVM, so you can't write code that let's you do this:


    That would require you to modify the String[] class, which you can't do.

    However, you can write a class that delegates to an array, and that does not allow direct access to that array from outside the class, and you can implement an Iterator that iterates over that array. Once you have done that, you have essentially created the ArrayList class, for this is what ArrayList does.

    We use Iterators because they provide a consistent way to iterate over any Collection. You can't use get(i) on a Set, for example. They allow us not to care about the implementation details of how to access a particular element of the collection, or keep track of which one we're currently looking at.
     
    Punit Jain
    Ranch Hand
    Posts: 1085
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    However, you can write a class that delegates to an array, and that does not allow direct access to that array from outside the class,

    You mean composition..??
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Punit Jain wrote:

    However, you can write a class that delegates to an array, and that does not allow direct access to that array from outside the class,

    You mean composition..??

    No, Jeff said what he meant (he usually does).

    You're question has been explained a few times; now it seems that you're just tossing out OO terms without much cohesion.

    What precisely is it that you don't understand about Iterators?

    Winston
     
    Punit Jain
    Ranch Hand
    Posts: 1085
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    That would require you to modify the String[] class, which you can't do.

    However, you can write a class that delegates to an array, and that does not allow direct access to that array from outside the class, and you can implement an Iterator that iterates over that array.


    well i am still confuse with this part, how to do this?
     
    Punit Jain
    Ranch Hand
    Posts: 1085
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeff means by this:

    public interface Iterator
    {
    boolean hasNext();
    Object next();
    }

    now i implement this in my array???
     
    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
    Punit Jain wrote:

    That would require you to modify the String[] class, which you can't do.

    However, you can write a class that delegates to an array, and that does not allow direct access to that array from outside the class, and you can implement an Iterator that iterates over that array.


    well i am still confuse with this part, how to do this?


    Punit Jain wrote:Jeff means by this:

    public interface Iterator
    {
    boolean hasNext();
    Object next();
    }

    now i implement this in my array???



    In my opinion, the easiest way to get an iterator for an object array, is to use the java.util.Arrays class to get a list, which in turn, used to get the iterator.

    Henry
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Punit Jain wrote:Jeff means by this:

    public interface Iterator
    {
    boolean hasNext();
    Object next();
    }

    now i implement this in my array???


    Think about it. You have an array. If somebody says "Give me the next element," and then shortly after that says, "okay, now, give me the next element," and so on, how would you do that? What do you need in order to keep track of your "current position" in an array? And what do you need to do to get the element at that position? And what do you need to do to advance to the "next" position?

    If you've ever done anything with an array, then you know how to do this.

    If you can't figure it out, look at the source code for java.util.ArrayList. (Or possibly an abstract superclass.) You would do it something like that.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!