The latest week assignment.16.12
Rewrite to be more concise by using the asList method and the LinkedList
constructor that takes a Collection argument.
for (String color : colors)
list1.add(color);
// add colors2 elements to list2
String[] colors2 =
{gold, white, brown, blue, gray, silver};
List list2 = new LinkedList();
for (String color : colors2)
list2.add(color);
That part is cut out of a long code. Here is all of the code just in case you
need to see it;
// Fig. 16.3: ListTest.java
// Lists, LinkedLists and ListIterators.
import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;
public class ListTest
{
public static void main(String[] args)
{
// add colors elements to list1
String[] colors =
{black, yellow, green, blue, violet, silver};
List list1 = new LinkedList();
for (String color : colors)
list1.add(color);
// add colors2 elements to list2
String[] colors2 =
{gold, white, brown, blue, gray, silver};
List list2 = new LinkedList();
for (String color : colors2)
list2.add(color);
list1.addAll(list2); // concatenate lists
list2 = null; // release resources
printList(list1); // print list1 elements
convertToUppercaseStrings(list1); // convert to uppercase string
printList(list1); // print list1 elements
System.out.printf(\%nDeleting elements 4 to 6…);
removeItems(list1, 4, 7); // remove items 4-6 from list
printList(list1); // print list1 elements
printReversedList(list1); // print list in reverse order
} ��
// output List contents
private static void printList(List list)
{
System.out.printf(\%nlist:\%n);
for (String color : list)
System.out.printf(\%s , color);
System.out.println();
}
// locate String objects and convert to uppercase
private static void convertToUppercaseStrings(List list)
{
ListIterator iterator = list.listIterator();
while (iterator.hasNext())
{
String color = iterator.next(); // get item
iterator.set(color.toUpperCase()); // convert to upper case
}
}
// obtain sublist and use clear method to delete sublist items
private static void removeItems(List list,
int start, int end)
{
list.subList(start, end).clear(); // remove items
}
// print reversed list
private static void printReversedList(List list)
{
ListIterator iterator = list.listIterator(list.size());
System.out.printf(\%nReversed List:\%n);
// print list in reverse order
while (iterator.hasPrevious())
System.out.printf(\%s , iterator.previous());
}
} // end class ListTest
16.18
Copying and Reversing LinkedLists) Write a program that creates a LinkedList
object of 10 characters, then creates a second LinkedList object containing a
copy of the first list, but in reverse order.

Purchase answer to see full
attachment




Why Choose Us

  • 100% non-plagiarized Papers
  • 24/7 /365 Service Available
  • Affordable Prices
  • Any Paper, Urgency, and Subject
  • Will complete your papers in 6 hours
  • On-time Delivery
  • Money-back and Privacy guarantees
  • Unlimited Amendments upon request
  • Satisfaction guarantee

How it Works

  • Click on the “Place Order” tab at the top menu or “Order Now” icon at the bottom and a new page will appear with an order form to be filled.
  • Fill in your paper’s requirements in the "PAPER DETAILS" section.
  • Fill in your paper’s academic level, deadline, and the required number of pages from the drop-down menus.
  • Click “CREATE ACCOUNT & SIGN IN” to enter your registration details and get an account with us for record-keeping and then, click on “PROCEED TO CHECKOUT” at the bottom of the page.
  • From there, the payment sections will show, follow the guided payment process and your order will be available for our writing team to work on it.