This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Just a simple for loop question

 
Lek Kin Wong
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why the second one work rather than the first one?
tomcat give me java.lang.ArrayIndexOutOfBoundsException: 2

 
Han Jie
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lek

Why would like you using filearray.length instead of filearray.length-1 with the second one?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays are zero based. And array length isn't equal to the last index of the array!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64699
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lek Kin Wong
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the Arrays class optionally with a Comparator. There is even a Oracle tutorial about it (here).
 
Lek Kin Wong
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic