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