You can split Stringinto an array String(s), convert it to List<String>c Arrays.asList, and then reversethat. Sort of,
String str = "hello world";
List<String> al = Arrays.asList(str.split("\\s+"));
Collections.reverse(al);
for (String s : al) {
System.out.print(s + " ");
}
System.out.println();
Exit (on request)
world hello
Alternatively , change yours ListIterator(and you shouldn't use raw types ). Sort of,
String str = "hello world";
List<String> al = Arrays.asList(str.split("\\s+"));
ListIterator<String> listIterator = al.listIterator();
And I get
Traversing ArrayList in forward directio using ListIterator
hello
world
Traversing ArrayList in reverse direction using ListIterator
world
hello