• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array Length In For Loop

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a array which is taking input from file "occupy.txt" which has element (no of people in a room)
4 5 0 2 1 0 6 7 8 And Below Programme Display Vacant Room In Reverse Order

everything is fine in this code But When I Change 2nd For Loop To It Starts Throwing Error
Thanks In Advance !!!
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
guest.length is 9, not 8.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to understand the difference between accessing the members of an array vs the total numbers of elements in an error.

Lets say you make an array of fingers. The length of this array would be 10 since we have ten fingers. However, when you want to access it one by one, you would have to go in the following manner:

0 1 2 3 4 5 6 7 8 9

Since it starts from 0, it ends at 9. So the 10th element in our array can be accessed as fingers[9] and not fingers[10]
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And there is another way of doing this:
reply
    Bookmark Topic Watch Topic
  • New Topic