int myArray1[] = {1,3,5,6,7,8,9,0};
myArray1 = new int[7];
for(int i=0;i<7;i++) {
System.out.println(myArray1[i]);
}
On the second line you're overwriting your populated array with a brand new array which has no values, but you're reserving seven spots in the array for values, but you aren't providing any initial values for those spots so the compiler assigns them the default value for the array's type. In the case of an int
the default value is 0
.
So, seven uninitialized int
values gives you seven zeroes.
Note: if you provide a list of values when you initialize the array you don't need to provide the length.
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.