• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 2908
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]
 
Sheriff
Posts: 8529
615
Mac OS X VI Editor BSD Linux
  • 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:
 
Whatever you say buddy! And I believe this tiny ad too:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic