简体   繁体   中英

Java Array Questions

So this is just a basic question. I was working with arrays in Java, figured out what I needed to do for my little assignment. I decided to play around with the array and see what would happen if I print out my array. I was very confused about the result I got with this code:

public class array_output{
    public static void main(String[] args){
       int[] anArray = new int[10];
       for(int p = 0; p < 10; p++){
           anArray[p] = p;
           //System.out.print(anArray[p] + " ");
           p++;
       }
       System.out.println (java.util.Arrays.toString(anArray));
    }
}

So the original intension was to just make an array "0, 2, 4, 6, 8". I decided to put my System.out.print outside of the for loop and print it out. The output I have got was

[0, 0, 2, 0, 4, 0, 6, 0, 8, 0]

Actually when I am writing this I thought that it is putting a "0" in place of the numbers that are not there between 0-9 like 0 is false and if the number shows it's in the array, not sure though.

If anyone could explain to me if that's what is happening here, and maybe explain how to print the values of the array outside of the for loop, I would greatly appreciate it

You're incrementing p twice at each iteration:

for(int p = 0; p < 10; p++) { // here
    anArray[p] = p;
    p++; // and here
}

Remove the p++ from the body of the loop.

If you want the array to contain 0, 2, 4, 6, ..., the body of the loop should be

anArray[p] = p * 2;

ie initialize every element of the array with the its position multiplied by 2.

You are incrementing p twice in each iteration of the for loop.
Therefore only the even indices of the array (0,2,4,6,8) are assigned.

for(int p=0; p<10; p++){ // <-- first time here
    anArray[p]=p;
    //System.out.print(anArray[p] + " ");
    p++; // <-- second time here
}

If you remove one of them (makes more sense to remove the second p++ ) it will work as expected.

BTW, if the goal is to assign only even values to the array, you should assign 2*p .

for(int p=0; p<10; p++){
    anArray[p]=2*p;
}

Your problem is, that you increment your loop variable p two times: One time in the regular for loop and a second time at the end of the for loop - therefore your index skips every two steps and the skipped array elements are not filled at all.

The solution is to iterate normally but place index * 2 at the current array position:

public class array_output{
        public static void main(String []args){
        int[] anArray = new int[10];

        for(int p=0; p<10; p++){
            anArray[p]=p * 2;
        }

        System.out.println (java.util.Arrays.toString(anArray));
    }
}

I recommend taking a look at the Java language specification

Quote:

Array components are unnamed variables that are created and initialized to default values (§4.12.5) whenever a new object that is an array is created (§10, §15.10). The array components effectively cease to exist when the array is no longer referenced.

In your case you are using int and those are initialized by default to 0.

That is the reason you get: [0, 0, 2, 0, 4, 0, 6, 0, 8, 0] Since the "missing" values are actually just not changed from their default value. The 2 4 6 8 is because you increment p twice, but I'm not sure if that is by design or if that's a bug since your initial question seems to indicate the former.

And if you just want it to contain: 0 2 4 6 8 You can use the following code:

public class array_output{
    public static void main(String[] args){
       int[] anArray = new int[5];
       for(int p = 0; p < 5; p++){
           anArray[p] = p * 2;
       }
       System.out.println (java.util.Arrays.toString(anArray));
    }
}

There's no reason to init the array to size 10 if you only want it to contain 5 elements.

Hi Nothing to worry on this, when you have declared array like:

int[] anArray = new int[10];

It got initilized '0' in all the index start from 0 to 9;

when you are inserting value it is inserting in:

anArray[0]=0;
anArray[2]=2;
anArray[4]=4;
anArray[6]=6;
anArray[8]=8;

And since in all other index value is already '0'. So, if you print the array it will print like:

[0, 0, 2, 0, 4, 0, 6, 0, 8, 0]

If you want to print array just run a for loop and print as:

for(int i=0;i<10;i++)
  System.out.println(anArray[i]);

By default, the int data type is initialized with the value 0. Since you increment p twice (once in the for loop, once in the body of the loop), only the elements with even indices get to have their value changed.

So in the first iteration you get anArray[0] = 0. Then you increment p twice so in the second iteration p is equal to 2 and you do anArray[2] = 2, while anArray[1] was skipped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM