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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

linked list. simple question. 2

Status
Not open for further replies.

capturedme

Programmer
Feb 12, 2007
1
US
Hi everyone. I have coded a simple program that is supposed to display the objects in the iterator, and as is, it does. I am trying to figure out a way to reverse the values so they read "Three, Two, One" instead of the other way around. Is there a way to do this without using a listiterator? Thanks a lot. Any help is appreciated.

Code:
import java.util.*;
public class Reverse {
  public static void main(String[] args) {
    List myList = new LinkedList();
    myList.add("One");
    myList.add("Two");
    myList.add("Three");
    for (int i = myList.size(); i >= 0; i--) {
      System.out.println(myList.toString());
    }
  }
}
 
Code:
import java.util.*;
public class Reverse {
  public static void main(String[] args) {
    List myList = new LinkedList();
    myList.add("One");
    myList.add("Two");
    myList.add("Three");
    for (int i = myList.size()-1; i >= 0; i--) {
      System.out.println(myList.get(i));
    }
  }
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
or
Code:
 import java.util.*;

public class ListReverse
{
	public static void main (String args[])
	{
		List <String> list = new ArrayList <String> ();
		for (String s : args)
		{
			list.add (s);
		}
		Collections.reverse (list);
		for (String s :list)
		{
			System.out.println (s);
		}
	}
}

don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top