aspose file tools*
The moose likes Beginning Java and the fly likes how do i do this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how do i do this" Watch "how do i do this" New topic
Author

how do i do this

melkor leprichaun
Greenhorn

Joined: Oct 31, 2003
Posts: 3
how do i read an arra backward
if i put 1 2 3 4 i want it to print 4 3 2 1
using this code
public class Echo {
public static void main (String[] args) {
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}
Lisa D'Aniello
Ranch Hand

Joined: Sep 25, 2003
Posts: 42
just reverse your loop:
for (int i = args.length; i >= 0; i--)
{
System.out.println(args[i]);
}
Hope this helps,
Lisa M.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Hi magical creature,
Welcome to JavaRanch!
Your "for" loop counts from 0 to args.length-1. Instead, make it count from args.length-1 to 0, and you'll have exactly what you want.


[Jess in Action][AskingGoodQuestions]
melkor leprichaun
Greenhorn

Joined: Oct 31, 2003
Posts: 3
thanks for your help it was very helpfull i changed it a little to get it to work here is working code.
public class Echo {
public static void main (String[] args) {
for (int i = args.length -1; i > -1; i--)

System.out.println(args[i]);
}
}
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986


Always use curley braces.


Please ignore post, I have no idea what I am talking about.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how do i do this