Results 1 to 4 of 4

Thread: ListTest

  1. #1
    kayln is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Question ListTest

    rewrite lines 16-25(bold and underlined) to be more concise by using the asList method and the LinkedList constructor that takes a Collection argument.

    import java.util.LinkedList;
    import java.util.List;
    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<String>list1 = new LinkedList<String>();

    for(String color: colors)
    list1.add(color);

    //add colors2 elements to list2
    String[] colors2 =
    {"gold", "white", "brown", "blue", "gray", "silver"};
    List<String>list2 = new LinkedList<String>();

    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);

    System.out.print("\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
    }//end main

    //output List contents
    private static void printList(List<String>list)
    {
    System.out.println("\nlist: ");

    for(String color: list)
    System.out.printf("%s", color);

    System.out.println();
    }//end method printList

    //Locate String objects and convert to upper case
    private static void convertToUppercaseStrings(List<String>list)
    {
    ListIterator<String>iterator = list.listIterator();

    while(iterator.hasNext())
    {
    String color = iterator.next();//get item
    iterator.set(color.toUpperCase());//convert to upper case
    }//end while
    }//end method covertToUppercaseString

    //obtain sublist and use clear method to delete sublist items
    private static void removeItems(List<String>list, int start, int end)
    {
    list.subList(start, end).clear();//remove items
    }//end method removeitems

    //print reversed list
    private static void printReversedList(List<String>list)
    {
    ListIterator<String>iterator = list.listIterator(list.size());

    System.out.println("\nReversedList");

    //print list in reversed order
    while(iterator.hasPrevious())
    System.out.printf("%s", iterator.previous());
    }//end method printReversed list
    }//end class ListTest


    Thank You all for help

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Thanks for posting your assignment. Do you have an actual question that's not, "here's my code and assignment, please fix it for me"?

  3. #3
    kayln is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default listTest

    for(String color: colors)
    list1.add(color);

    //add colors2 elements to list2
    String[] colors2 =
    {"gold", "white", "brown", "blue", "gray", "silver"};
    List<String>list2 = new LinkedList<String>(Array.asList(colors2));

    for (String color: colors2)
    list2.add(color);
    if i put asList there, i get error. what other change should i make?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by kayln View Post
    for(String color: colors)
    list1.add(color);

    //add colors2 elements to list2
    String[] colors2 =
    {"gold", "white", "brown", "blue", "gray", "silver"};
    List<String>list2 = new LinkedList<String>(Array.asList(colors2));

    for (String color: colors2)
    list2.add(color);
    if i put asList there, i get error. what other change should i make?
    You would need to call this method on the java.util.Arrays class not the Array class. You will either need to import this class and call it similar to how you're doing it:

    Java Code:
    List<String>list2 = new LinkedList<String>(Array[color="red"][b]s[/b][/color].asList(colors2));

    or call it with its fully qualified name:

    Java Code:
    List<String>list2 = new LinkedList<String>(java.util.Array[color="red"][b]s[/b][/color].asList(colors2));

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •