top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Please explain in what way Get container with Iterator from ArrayList ?

+1 vote
319 views
Please explain in what way Get container with Iterator from ArrayList ?
posted Jan 21, 2016 by Joy Nelson

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MainClass {
  public static void main(String[] args) {
    List cats = new ArrayList();
    for (int i = 0; i < 7; i++)
      cats.add(new Cat(i));
    Iterator e = cats.iterator();
    while (e.hasNext())
      ((Cat) e.next()).id();
  }
}

class Cat {
  private int catNumber;

  public Cat(int i) {
    catNumber = i;
  }

  public void id() {
    System.out.println("Cat #" + catNumber);
  }
}

/*OUT PUT*/

Cat #0
Cat #1
Cat #2
Cat #3
Cat #4
Cat #5
Cat #6
answer Jan 22, 2016 by Karthick.c
...