Question: Practice using the iterator in a loop and possibly removing elements from the list using the iterator. Use an iterator to remove all elements containing

Practice using the iterator in a loop and possibly removing elements from the list using the iterator.

Use an iterator to remove all elements containing '3'.
Expected output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
[1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 29, 40, 41, 42, 44, 45, 46, 47, 48, 49, 50]
*/
import java.util.LinkedList ;
import java.util.ListIterator ;
import java.util.Scanner ;

public class LinkedListTester10
{
public static void main(String[] args)
{
LinkedList list = new LinkedList() ;
for (int i = 1 ; i <= 50 ; i++) {
list.add(i + "") ;
}
System.out.println(list) ;
//-----------Start below here. To do: approximate lines of code = 4
// 1-4. Use an iterator to remove all elements containing "3"





//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
System.out.println(list) ;
System.out.println("Expected:[1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 29, 40, 41, 42, 44, 45, 46, 47, 48, 49, 50]");
}
}


P.S: Please make sure the output is same as expected and the code is formatted well here. 

Step by Step Solution

3.35 Rating (142 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

import javautilIterator import javautilLinkedLi... View full answer

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 Programming Questions!