Java Reference
In-Depth Information
{
System.out.println(“odds[“ + i + “] = “
+ odds[i]);
}
System.out.println(“** odds the second time **”);
int [] temp = odds;
odds = new int[20];
System.arraycopy(temp, 0, odds, 4, temp.length);
for(int i = 0; i < odds.length; i++)
{
System.out.println(“odds[“ + i + “] = “
+ odds[i]);
}
}
}
In the ArrayCopyDemo program, the odds reference is assigned to the temp
reference:
int [] temp = odds;
At the time of this assignment, odds is referring to an array of 10 ints. There-
fore, temp is also referring to the array of 10 ints. Then, odds is assigned to a
new array of 20 ints, all of which are initially 0. The first 10 elements in temp
(which is all of the elements in temp) are copied into the new array of 20 ints,
starting at the element at index 4. Notice in the output in Figure 9.3 that the
other elements in odds are 0 because they have not been assigned a value yet.
Figure 9.3
Output of the ArrayCopyDemo program.
Search WWH ::




Custom Search