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

    Default Cleaning up my linked list

    Hello everyone,

    Ive been experimenting with Linked Lists and feel I have a grasp on it but by no ways do I understand it in full. I was told I could make this code more concise by using the asList method and a linked list constructor that takes a collection as an argument. Im not to familiar with the asList method.

    Any suggestions in how I should go about using this?
    Within my code I used asList in two lines but they show up as comments right now becuase Im not sure if I did it right.

    package Phase4LinkedList;
    import java.util.*;

    /**
    *
    * @author Morgan 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 and manipulates two Linked List objects
    public ListTest(){


    List <String> list1 = new LinkedList <String>();
    List <String> list2 = new LinkedList <String>();

    //List < String > list1 = Arrays.asList("black", "yellow", "green",
    //"blue", "gray", "silver");

    //List <String> list2 = Arrays.asList("gold", "white", "brown", "blue",
    //"gray", "silver");

    //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

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

    Default

    Ok so I made a little process, got the asList method running. Now how do I use a Linked List constructor that uses a Collection?
    Here is my modified code

    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 and manipulates two Linked List objects
    public ListTest(){

    List list1 = Arrays.asList(colors);
    List list2 = Arrays.asList(colors2);

    //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
    25

    Default

    For the asList method, have a look at the Arrays API. It allows you to translate an array into a collection which can then be used in the constructor of each List.

    Best of luck.

    Never mind the above. To use a LinkedList constructor that uses anything you of course go to the API, right?

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

    Default

    Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. You may want to click on the Preview tab to make sure that your code is formatted correctly. Another way is to place the tag &#91;code] at the top of your block of code and the tag &#91;/code] at the bottom, like so:

    Java Code:
    &#91;code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    &#91;/code]

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

    Default

    No reply and then double posted next day.

  6. #6
    pussianny is offline Member
    Join Date
    Feb 2009
    Location
    Canada
    Posts
    1
    Rep Power
    0

    Default act guidance how to subject to during crisis

    To me of 18 years. My guy act ons as the foreman in close shop. To it already declared reduction of the stick, and that whom pleasure not decrease, desire stoop the earnings on 20 %. Certainly it on the one disposal is not a lot of, and with another it seems to me no greater than the opening! And article has across across, there to be resulted the book of 10 not most demanded trades during critical time! I consequently too there is, and at you on act on something has exchanged after disaster? And how you in non-specialized drawing it to go through? Also what I can succeed a do over to advance my guy at least?

Similar Threads

  1. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 08:53 PM
  2. Help with Doubly linked list
    By Dr Gonzo in forum New To Java
    Replies: 5
    Last Post: 12-06-2008, 07:45 AM
  3. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 03:15 AM
  4. Linked List
    By rnavarro9 in forum New To Java
    Replies: 0
    Last Post: 11-29-2007, 03:42 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
  •