aspose file tools*
The moose likes Beginning Java and the fly likes Just a simple for loop question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Just a simple for loop question" Watch "Just a simple for loop question" New topic
Author

Just a simple for loop question

Lek Kin Wong
Ranch Hand

Joined: Feb 23, 2010
Posts: 30
why the second one work rather than the first one?
tomcat give me java.lang.ArrayIndexOutOfBoundsException: 2

Han Jie
Greenhorn

Joined: Sep 24, 2010
Posts: 24
Hi Lek

Why would like you using filearray.length instead of filearray.length-1 with the second one?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Arrays are zero based. And array length isn't equal to the last index of the array!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60071
    
  65

Please take the time to choose an appropriate forum for your posts. This forum is for questions on Servlets. For more information, please click this link ⇒ CarefullyChooseOneForum.

This post has been moved to a more appropriate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Lek Kin Wong
Ranch Hand

Joined: Feb 23, 2010
Posts: 30
Abimaran Kugathasan wrote:Arrays are zero based. And array length isn't equal to the last index of the array!

many thanksss!
so how do I sort array values in descending order?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You can use the Arrays class optionally with a Comparator. There is even a Oracle tutorial about it (here).


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Lek Kin Wong
Ranch Hand

Joined: Feb 23, 2010
Posts: 30
Wouter Oet wrote:You can use the Arrays class optionally with a Comparator. There is even a Oracle tutorial about it (here).


I just solve the problem with Arrays class, Thank.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36563
    
  16
Lek Kin Wong wrote: . . . I just solve the problem with Arrays class, Thank. . . .
Well done. Please give us a bit more detail about what you did, in case anybody else has the same problem later.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Just a simple for loop question
 
Similar Threads
Finding a folder or a file
Serving PDF with my servlet - performance & non-display
trouble with Runtime.exec()
How to zip files and folders in a directory into 1 zip file?
FTP Woes! -Solved