Question: Given one class LinkedListExample, you are first to test this class and then do the following: Delete the two for-loop statements; Rewrite code in lines
Given one class LinkedListExample, you are first to test this class and then do the following: Delete the two for-loop statements; Rewrite code in lines 13 and 21 (that is, the creation of list objects list1 and list2) with using the Arrays.asList() method (i.e., passing this method as to the LinkedList constructor). (The outputs should be the same as the ones from the original program.)
import java.util.List; import java.util.LinkedList; import java.util.ListIterator;
public class LinkedListExample { public static void main( String[] args ) {
String[] colors = { "black", "yellow", "green", "blue", "violet", "silver", "red" }; List< String > list1 = new LinkedList< String >();
for ( String color : colors ) list1.add( color );
String[] colors2 = { "gold", "white", "brown", "blue", "gray", "silver", "indigo" }; List< String > list2 = new LinkedList< String >();
for ( String color : colors2 ) list2.add( color );
list1.addAll( list2 ); list2 = null; printList( list1 );
convertToUppercaseStrings( list1 ); printList( list1 );
System.out.print( " Deleting elements 3 to 5..." ); removeItems( list1, 3, 6 ); printList( list1 ); printReversedList( list1 ); } private static void printList( List< String > list ) { System.out.println( " list: " ); for ( String color : list ) System.out.printf( "%s ", color );
System.out.println(); } private static void convertToUppercaseStrings( List< String > list ) { ListIterator< String > iterator = list.listIterator();
while ( iterator.hasNext() ) { String color = iterator.next(); iterator.set( color.toUpperCase() ); } } private static void removeItems( List< String > list, int start, int end ) { list.subList( start, end ).clear(); } private static void printReversedList( List< String > list ) { ListIterator< String > iterator = list.listIterator( list.size() );
System.out.println( " Reversed List:" );
while ( iterator.hasPrevious() ) System.out.printf( "%s ", iterator.previous() ); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
