Results 1 to 3 of 3
  1. #1
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default Calling a linked List Constructor with a collection parameter

    Hello everyone,

    Still rather new to java and Im trying to make this code more concise by adding the asList method and then passing it to the LinkedList constructor that takes a collection as a parameter. Im not quite understanding this. I got the asList area working but dirrectly below that I try to call the constructor and im getting an error. Where am I going wrong?? Here is a section of my code that deals with this problem. If you need the whole code let me know and Ill upload it.

    //creates an array to store 6 Strings
    private static final String colors[] = {"black", "yellow", "green",
    "blue", "gray", "silver"};

    //creates an array to store 6 Strings
    private static final String colors2[] = {"gold", "white", "brown",
    "blue", "gray", "silver"};

    //Creates two lists using the asList method
    List list1 = Arrays.asList(colors);
    List list2 = Arrays.asList(colors2);
    ListTest(list1);
    ListTest(list2);

    //Creates and manipulates two Linked List objects
    public ListTest(List list){

    //adds elements to the list "link"
    for (String color : colors)
    list1.add(color);

    //add elements to the list "link2"
    for (String color : colors2)
    list1.add(color);

    // concatenate lists
    list1.addAll(list2);

    //release resources
    list2 = null;

    //print list1 elements
    printList(list1);

    //Convert to uppercase Strings
    convertToUpperCaseString(list1);
    printList(list1);

    System.out.print( "Deleting elements 4 to 6...");

    //removes items 4-7 from the list
    removeItems(list1, 4, 7);

    printList(list1);

    printReversedList(list1);
    }//end ListTest constructor

  2. #2
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    Disregard that last post I got a little farther.
    Is the code posted below accurately using the asList metod and then using a linked List constructor with a Collection argument??

    package Phase4LinkedList;
    import java.util.*;

    /**
    *
    * @author Morgan W. Leavitt
    */
    public class ListTest {

    //creates an array to store 6 Strings
    private static final String colors[] = {"black", "yellow", "green",
    "blue", "gray", "silver"};

    //creates an array to store 6 Strings
    private static final String colors2[] = {"gold", "white", "brown",
    "blue", "gray", "silver"};

    //Creates two lists using the asList method
    List list1 = Arrays.asList(colors);
    List list2 = Arrays.asList(colors2);

    //Creates and manipulates two Linked List objects
    public ListTest(List list1, List list2){

    this.list1 = list1;
    this.list2 = list2;


    //adds elements to the list "link"
    for (String color : colors)
    list1.add(color);

    //add elements to the list "link2"
    for (String color : colors2)
    list1.add(color);

    // concatenate lists
    list1.addAll(list2);

    //release resources
    list2 = null;

    //print list1 elements
    printList(list1);

    //Convert to uppercase Strings
    convertToUpperCaseString(list1);
    printList(list1);

    System.out.print( "Deleting elements 4 to 6...");

    //removes items 4-7 from the list
    removeItems(list1, 4, 7);

    printList(list1);

    printReversedList(list1);
    }//end ListTest constructor

    //outputs list contents
    public 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 uppercase
    private void convertToUpperCaseString(List <String> list){

    ListIterator <String> iterator = list.listIterator();

    while (iterator.hasNext()){

    //get item
    String color = iterator.next();
    iterator.set(color.toUpperCase());
    }//end while
    }//end method convertToUpperCaseString

    //obtain sublist and ise clear method to delete sublist items
    private void removeItems(List <String> list, int start, int end){

    //remove items
    list.subList(start, end).clear();
    }//end method removeItems

    //print reversed list
    private void printReversedList(List <String> list){

    ListIterator <String> iterator = list.listIterator(list.size());

    System.out.println("\nReversed List:");

    while (iterator.hasPrevious())
    System.out.printf("%s ", iterator.previous());
    }//end method printReversedLine

    public static void main(String args[]){

    new ListTest();
    }//end method main

    }//end class ListTest

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

    Default

    So rather than reply to posts made in your thread yesterday, you decide to ignore these posts and instead post a new thread? That won't gain you many friends here.

Similar Threads

  1. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 08:53 PM
  2. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 03:15 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  5. Help with linked list
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:29 AM

Posting Permissions

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