Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Array Length In For Loop  RSS feed

 
Vinay Tripathi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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 !!!
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
guest.length is 9, not 8.
 
salvin francis
Bartender
Posts: 1614
36
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • 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]
 
Liutauras Vilda
Marshal
Posts: 4670
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And there is another way of doing this:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!