Question: There are two simple ways to traverse a list: using an iterator and using indexing and the get() method. For example the following two methods

There are two simple ways to traverse a list: using an iterator and using indexing and the get() method. For example the following two methods will produce the same output regardless of the dynamic type of the list passed in as a parameter:

public static void printListOne(List list)

{

for (ListIterator iterator = list.listIterator();

iterator.hasNext(); ) {

System.out.println(iterator.next());

}

}

public static void printListTwo(List list)

{

for (int i = 0; i < list.size(); ++i) {

System.out.println(list.get(i));

}

}

However, the claim is made that "printListOne" is more efficient for LinkedList than "printListTwo". Both algorithms perform the same for ArrayList objects, however. Provide an explanation for the two claims above.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!