Computer Program Reading Comprehension Project
U of T Computer Program Reading Comprehension Project
Array Operation Exercise
This is an exercise about array operations such as array declaration, construction, initialization and high-performance copy.

Download files: ArrayOperation.java
Areas: Intro to arrays.


ArrayOperation.java
1class ArrayOperation {
2
3   public static void main(String args[]){
4     int source[]= new int[6];
5      for (int i=0;i<6;i++){
6       source[i]=i+1;
7     }
8
9     //Array Initialization
10     int destination[]={10,9,8,7,6,5,4,3,2,1};
Displaying 10 of 17 lines
View full source


Question 1
Does it make any difference if we replace line 4:
int source[]= new int[6];
with
int source[6];?
Hints Solution

Question 2
Can we replace
int source[]= new int[6];
with
int source[];
because we will initialize the array elements in the for-loop?
Hints Solution

Question 3
In the source array which is declared and constructed in
int source[] = new int[6];,
is source[6] a valid array element?
Hints Solution

Question 4
In int destination[]={10,9,8,7,6,5,4,3,2,1};, the destination array is declared, constructed and initialized. If we would like to achieve the same effect using a for-loop, what would the code look like?
Solution

Question 5
What does the following line of code do?
System.arraycopy(source,0,destination,0,source.length)
Solution

Question 6
Can we to achieve the same effect as
System.arraycopy(source,0,destination,0,source.length)
using a for-loop? What would the code look like?
Solution

Question 7
Please list the values of the destination array elements after the call to the arraycopy method.
Hints Solution