René:
when you want to use an array there are 3 steps that you should follow:
declare the array
int [] ages;
define the size:
ages = new int [10]; // at this point you just define the max size of the
array
the third step is to fill the array, that mean that you should start on
position 0 to 9 (you start on zero and you define the max size as 10 )
for example ages[0] = 100;
now ... answering your question what you should do is the following:
public class ArrayTest
{
public static void main(String[] args) {
// Declare and create new int array whose size is 10
int[] ages = new int[10];
int j = 100;
int count = 0;
//before you display your array you should fill it.
for (int i = 0; i< ages.length; i++)
{
ages[i] = j;
j ++;
}
// now that you have your array filled you can displayed.
// Display the values of each entry in the array
// count variable will help you to obtain each value of your array (start on
ages [0] and moves until ages[9] that is the last element of your array)
// you could either use while or for controls to perform this.
while (count<ages.length)
{
System.out.println(ages[count] );
count++;
}
}
}
regards,
Olivia
On Wed, Jan 5, 2011 at 11:01 AM, Rene Olgers <[email protected]> wrote:
> Hi Michele,
>
> thank you for your email.
> In this case I try to assign a value of 100 to the first entry, 101 to
> the second and etc.
> Instead of assigning all values by hand I want to use a loop.
>
> Could you please assist?
>
> Thanks,
>
> René
>
> On 4 jan, 07:17, Michèle Garoche <[email protected]> wrote:
> > On Jan 4, 12:25 am, Rene Olgers <[email protected]> wrote:
> >
> >
> >
> > > public class ArrayTest {
> >
> > > /**
> > > * @param args the command line arguments
> > > */
> > > public static void main(String[] args) {
> > > // Declare and create new int array whose size is 10
> > > int[] ages = new int[10];
> > > int j = 100;
> > > int count = 0;
> >
> > > // Display the values of each entry in the array
> > > while (count<ages.length){
> > > System.out.println(ages[j] );
> > > count++;
> > > j++;
> >
> > > }
> > > }
> >
> > > }
> >
> > To complete what others have already said, it would be probably better
> > to fill your array with meaningful numbers, unless you insist in
> > having 0 (the default value when none is supplied) in all elements, as
> > you have just declared the array (that is allocated memory for it and
> > initialize all elements to default value), but you have not
> > initialized it (giving each element a value).
> >
> > In this case, just add at the top of the loop:
> > ages[j] = j;
> > to get 0,1, ... 9 for example
> >
> > Michèle Garoche
>
> --
> To post to this group, send email to
> [email protected]
> To unsubscribe from this group, send email to
> [email protected]<javaprogrammingwithpassion%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
>
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en