The Artima Developer Community
Sponsored Link

Java Answers Forum
Newbie - Simple Array Question

3 replies on 1 page. Most recent reply: Jun 8, 2003 7:47 PM by Alan Sinclair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Alan Sinclair

Posts: 2
Nickname: auzzie
Registered: Jun, 2003

Newbie - Simple Array Question Posted: Jun 3, 2003 9:33 PM
Reply to this message Reply
Advertisement
public class example1 {
public static void main( String args[] ) {

int numarray[] = new int[10];

for (int indx = 0; indx < 5; indx++) {

numarray[indx + 2] = indx + 1;

}

System.out.println(numarray[4]);
}

}

forgive my ignorance, but i cant work out the line:

numarray[indx + 2] = indx + 1;

when i insert a system.out.print line inside the for loop it shows me all the values in the array. these are (0, 0, 1, 2, 3) so the numarray[4] value is obviousley 3. but how can there be 2 leading 0's in the array? i think i must be reading the line (numarray[indx + 2] = indx + 1;) totally wrong.

could anyone please step through the line of code, I would greatly apperciate it. again sorry about the 'level' im at, but i guess i got to start some where :)

thanks, Alan


John Channing

Posts: 17
Nickname: drc
Registered: Jun, 2003

Re: Newbie - Simple Array Question Posted: Jun 5, 2003 5:23 AM
Reply to this message Reply
Hi Alan,
The default value of int is zero. Since you haven't allocated a value to the first two elements in the array they are zero.
John

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Newbie - Simple Array Question Posted: Jun 7, 2003 9:33 AM
Reply to this message Reply
In the line:
int numarray[] = new int[10];

you create an array of length 10 of int values.
numarray[0]
numarray[1]
numarray[2]
numarray[3]
numarray[4]
numarray[5]
numarray[6]
numarray[7]
numarray[8]
numarray[9]


The default value of a int variable is 0, so initially each array element is 0.

Your for loop:
for (int indx = 0; indx < 5; indx++) {
 
numarray[indx + 2] = indx + 1;
 
}
 

when indx is 0, numarray[2] is set to indx+1 or 1
when indx is 1, numarray[3] is set to indx+1 or 2
when indx is 2, numarray[4] is set to indx+1 or 3
when indx is 3, numarray[5] is set to indx+1 or 4
when indx is 4, numarray[6] is set to indx+1 or 5

then the for loop exits

and the line

System.out.println(numarray[4]);

executes
giving 3


now your array values are:
numarray[0] is still 0
numarray[1] is still 0
numarray[2] is 1
numarray[3] is 2
numarray[4] is 3
numarray[5] is 4
numarray[6] is 5
numarray[7] is still 0
numarray[8] is still 0
numarray[9] is still 0

Alan Sinclair

Posts: 2
Nickname: auzzie
Registered: Jun, 2003

Re: Newbie - Simple Array Question Posted: Jun 8, 2003 7:47 PM
Reply to this message Reply
Thanks for taking the time to reply Charles & John, that was a big help. Makes a lot more sense to me now.

cheers,

Alan

Flat View: This topic has 3 replies on 1 page
Topic: How to add a image as a background Previous Topic   Next Topic Topic: Help: How reload a built file ??

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use