Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

i thought this would be a great place

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
i thought this would be a great place to get some help...

i need to create a java application that creates a linked list containing 5 String objects, i need to step through the list starting at the first printing out each string to the screen, then start at the end and print out each string to the screen until i'm back at the top of the list.

i am an extreme newbie, and am just trying to teach myself, anybody care to help?
 
Hi,
Here is the code that does the job for u..

import java.util.*;

class Lister
{
public static void main(String[] args)
{
/** make a new linked list */
LinkedList list = new LinkedList();

/** add five strings to the linked list */
for (int i = 0; i < 5 ; i++)
{
list.add(i,&quot;String&quot;+i);
}

/** move through the list and print out the elements from 1 to 5 */
System.out.println(&quot; From first to last &quot;);
for (int i = 0 ; i < 5 ; i++)
{
System.out.println(&quot; Element No &quot;+i+&quot; &quot; + (String)list.get(i));
}

/** move through the list and print out the elements from 5 to 1 */
System.out.println(&quot;\n From last to first &quot;);
for (int i = 4 ; i >= 0 ; i--)
{
System.out.println(&quot; Element No &quot;+i+&quot; &quot; + (String)list.get(i));
}
}
}

Hope this helps,
Reddy316
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top