Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Length Of the Array

Status
Not open for further replies.

munnanext

Programmer
Aug 20, 2004
49
US
I am of the opinion that arry elements are indexed from 1 to size of the array, But in some book it is given that array elements are indexed from 0 to (length of the array -1)
Can you tell me which is the correct method?
Appreciate your help
 
my understanding is that the latter (0-based) is correct. That is, when I initialize an array, the first index is 0.

Bob Rashkin
rrashkin@csc.com
 
Its 0.

Be care when looping array values not to go past the end :

Code:
int[] arr = new int[5];
//set values somehow

for (int i = 0; i < arr.length; i++) {
  // do something
}

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top