View RSS Feed

Algorithm and Data Structure

Algorithm and data structure examples in Java.

  1. What is the advantage of using an Iterator compared to the get(index) method?

    by , 02-19-2012 at 07:55 AM
    One of the following methods should be used to iterate the List.

    1. Iterator
    2. get(index)

    get(index) method is quite slower in comparison with Iterator.

    Consider this example:

    Java Code: This is the code to explain Iterator
    public class TestClass {
      public static void main(String[] args) {
        int  len = 100000;
        LinkedList linkedLst = new LinkedList(); 
        ArrayList  arrayLst = new ArrayList();
        for (int m =0; m!= len; m++)
    ...
    Categories
    Linked List