Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need some help with java lists!

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need some help with java lists!

    Hi, everybody! I am new to java and really hope that someone helps me.
    Based on the following code, i need to change it to have :
    2 lists with names ,first list with 10 names and the other 3. And the output to be 3 names from the first list and 1 from the second. If someone helps me i would really appreciate. THANK YOU



    // 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< 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 ); // print list1 elements

    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 uppercase
    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 convertToUppercaseStrings

    // 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( "\nReversed List:" );

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help with java lists!

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Please explain where you are having any problems.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Lisp Lists in java
    By ir9 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 21st, 2013, 10:58 PM
  2. Linked Lists Java Programming HELPP P.S. I am a beginner in Java Programming
    By judemartin99 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 20th, 2013, 02:19 PM
  3. Array Lists help!!
    By lilika in forum What's Wrong With My Code?
    Replies: 17
    Last Post: January 4th, 2011, 09:21 AM
  4. iterators & linked lists in java question
    By somewhat_confused in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 4th, 2010, 10:59 AM
  5. Lists of Sets and Sets of Lists
    By Newoor in forum Collections and Generics
    Replies: 2
    Last Post: December 8th, 2009, 08:13 PM

Tags for this Thread