iterator « ArrayList « Java Collection Q&A

Java Collection Q&A
3.Array Byte
4.Array Char
5.Array Convert
6.Array Dimension
7.Array Integer
8.Array Object
9.Array String
14.Garbage Collection
Java Collection Q&A » ArrayList » iterator 

1. traditional for loop vs Iterator in Java

Is there any performance testing results available in comparing traditional for loop vs Iterator while traversing a ArrayList,HashMap and other collections? Or simply why should I use Iterator over for loop or ...

2. How can I determine the type of object an ArrayList iterator will return next?

I have an ArrayList and I am using an iterator to run through it. I need to find out what type of object is next:

Iterator vehicleIterator = vehicleArrayList.iterator();


3. Why can't I call specific class methods on an Iterator?

ArrayList array = new ArrayList();

Iterator it1 = array.iterator();
while (it1.hasNext()){
   Myclass temp = it1.myGetterMethod();
   System.out.println (temp);
This is what I would like to implement, but Iterator only returns a ...

4. Java: The difference between iterators and arraylists

How would you explain to someone who has just started programming in Java, what the difference between ArrayLists and Iterators are? Why I would use an iterator instead of using the get() ...

5. Java - problems iterating through an ArrayList

Ok so I have an ArrayList (arrBok), which is full of book objects (the code is in Norwegian, so pay no attention to that please). I want to make a public ...

6. How to use an iterator in this case?

I have to iterate through an arraylist in this manner.

ArrayList<Integer> li = new ArrayList<Integer>();

for (int i = 0; i < li.size() - 1; i++)
    System.out.println(li.get(i) + " " ...

7. What happens if you call the same iterator twice on the same collection?

If I set up an iterator for myList:

Iterator iter = myList.iterator();
    MyObj myObj = (MyObj);
And I call it a second time:

8. Need help in specific scenario , Iterate ArrayList with multiple condition in java

Scenario : An Arraylist containing following DTOs'

    MetadatRetrievalDTO[] dto  = new MetadatRetrievalDTO[16];

    dto[0] = new MetadatRetrievalDTO();

9. Java Iterable> to ArrayList>

Maybe I am missing something but I thought that If I declare my class as such:

public class Something<T> implements Iterable<Iterable<T>> {
  public Something(Iterable<Iterable<T>> input) {
I should be able ...

10. Creating a remove() method to remove an item from an arraylist (Java)

I have 2 classes Student class and StudentTest class. In my Student class I need to write a method called remove(int ID) to remove a student with a specific ID, from ...

11. Implementing an iterator for a stack

I defined an interface StringStack

public interface StringStack{
    //add a value to the stack
    public void push(String value);
    //fetch top-most element of the ...

12. Remove elements from ArrayList - Code is skipping elements

What I am trying to do is to loop through a list of indices that refer to a paragraph somewhere. If there paragraph is too short, remove the index from the ...

13. using two iterators in one ArrayList

Edit: Thank you for all your prompt replies. Now I see the assignment won't work. From another thread I read that iterator in Java is much less powerful than iterator in ...

14. UnsupportedOperationException in AbstractList.remove() when operating on ArrayList

ArrayList's list iterator does implement the remove method, however I get the following exception thrown: UnsupportedOperationException at java.util.AbstractList.remove( By this code:

protected void removeZeroLengthStringsFrom(List<String> stringList)
    ListIterator<String> iter = stringList.listIterator();

15. How to Iterate Through an Array List

So right now I have a program containing a piece of code that looks like this...

while (arrayList.iterator().hasNext())
     if( arrayList.iterator().next().equals(value))  //value is equal to a String value

16. How to Iterate Through Values in Properties File in Java

so I was wondering if anyone knew how I would go about reading in mutliple values from a key, delimiting them by commas and storing them to an arraylist from a ...

17. Fetch Matching Subset from ArrayList

I have a ArrayListof some POJO. Let say Employee. Then I am getting another ArrayList which contains only 'id' of that POJO.(i.e. Employee) Now, I want a sub list of matching ids from ...

18. Adding elements to ArrayList while using Iterator in Java

I had a problem with adding elements to my ArrayList while using an Iterator. In the following code it gives me this output:

But still it misses the one I had added ...

19. Matching Two List values and finally get a final List with distinct value

I have question here. I have two list and both this list have some common elements. This common elements along with the values has to be put in another List. which is ...

20. arrayList iterator problems

Hi, With a little help I've finally managed to add an arrayList to my swing program. The problem is now with iteration through this list. When I try to go through the list, it simply jumps straight to the beginning or end of the list, depending on whether I click the backwards or forwards button. The arrayList is printing out correctly, ...

21. what is the use of iterator() method in ArrayList..?

You would use a Collection to allow you room to change which implementation you wanted to use. Suppost originally all you needed was something to hold a list of objects, well an ArrayList will do fine. But then you realise that the values of the objects in this collection must be unique so you change it to a Set. If you ...

22. How to give multiple iterator in Arraylist?

hi Team i want to know that can i give an iterator to an arraylist, that starts from a dynamic location in the arraylist. In my application, there is one arraylist, that contains a number of objects. i want to take each object and compare that object with the remaining objects in the arraylist. for example, take the first object, compare ...

23. Need to write ArrayList class & Iterator interface in jdk1.1.8

If you open the rt.jar file with WinZip or FileLibrarian or whatever you use, just extract the class files that you want and put them with your class files. Be carefull though, because you need to research to take all the other classes that those 2 rely on to be sure that it works.

24. Iterator with an ArrayList

ok, so that makes more sense. If I don't go to the next element, it will stay there forever, but by calling next() it actually brings me to the next element. It was looping before, because it was never making it to the next element. Thanks again. [ July 07, 2005: Message edited by: Nicholas Carrier ]

25. ArrayList with Iterator

27. iterator() in ArrayList

Hi I have one query , I have seen in the java doc , ArrayList inherited iterator() from its super class java.util.AbstractList and from interface java.util.List. I am not able to understand why it is inheriting same method from different place ? Could ny body explain me what is the reason ? regards, S

28. Array list and Iterator

Good evening, can I ask for some guidance with an issue I have with a small program? I am having problems with a couple of classes using an ArrayList and an Iterator with a scanner reading in a file. When the file has been read in and another class attempts to return the elements in the Iterator, it only reports back ...

29. How can i add Object into ArrayList after assigning Iterator

Hi there, Please refer following code snippet. 1 List arrayList = new ArrayList(); 2 arrayList.add(new Integer(24)); 3 arrayList.add(new Double(2)); 4 arrayList.add(new Employee( Employee.getNewEmployeeId(), "Manager"); 5 ListIterator iterator = arrayList.listIterator(); 6 arrayList.add(new Employee(Employee.getNewEmployeeId(), "Engineer"); This code throws java.util.ConcurrentModificationException as i am trying to add Object after assigning iterator to the List. I found that we have to use add method of iterator, ...

30. Iterators and array List

31. Iterator for ArrayList

craq wrote: bingo :-D thanks welcome. just for interest, what's the difference between awt and util? (Or where could I read about that?) I'm used to netBeans sorting that stuff out for me. It's time to leave the NetBeans womb and venture out into the wild world; this includes having a look at the API and the Sun tutorials. My recommendation ...

32. Working with ArrayList Iterator

Thanks for the response, i'll give the suggestions you made a try. I'm not iterating child twice in the code, at least i didn't think I was! What I was trying to do was take the arraylist of children from the person and iterate through that list so that all of those children would be added to the descendants list. Once ...

33. ArrayList, Iterator, and objects

Hi all, I got a couple questions regarding ArrayList, Iterator, and objects. How would you store the results of an SQL result set in an ArrayList using the setters and getters from an object (MyItems). This MyItems class has attributes that correspond directly to the items in the database (title, ISBN, author, etc). Basically, I need take the results from an ...

34. Iterator vs ArrayList.get(int index)

35. ArrayList: what is faster get(int index) or iterator

Are you asking whether it's faster to use an iterator or get(index) in a for loop? Or are you asking whether it's faster to use an iterator or get(index) to get a single value? If the former...then just use the iterator. Even if one could demonstrate a speed improvement with get(index) (which seems unlikely) it's not worth it because you're making ...

36. Creating an iterator to iterate the arraylist. and printing

ArrayList arrayList = new ArrayList(); arrayList.add(new Rectangle()); arrayList.add(new Rectangle(22, 57)); arrayList.add(new Circle()); arrayList.add(new Circle(11)); HashMap map = new HashMap(); Iterator iter = arrayList.iterator(); int count = 0; while (iter.hasNext()) { String key = "ID_" + ++count; Object value =; map.put(key, value); System.out.println("(Added " + key + ") " + value); } BufferedReader br = new BufferedReader(new InputStreamReader(; while (true) { ...  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.