|
AP Exam Practice
Questions on Arrays
Practice Problem 1)
You can assume that the myArray1 contains elements myArray1[0], myArray1[1], myArray1[2].... myArray1[m-1] where m = myArray1.length.
If myArray1 originally stored 0,4,22,0,0,4,0,11,11,0,0,0,4 what will myArray2 contain after executing teh cod segment below?
int counter=0;
for(int i=0;i < m ; i++){
if(myArray1[i] != 0 )
{
myArray1[counter]= myArray1[i];
counter++;
}
int[] myArray2;
myArray2 = new int[counter];
for(int i=0;i< counter; i++)
myArray2[i] = myArray1[i];
|
This segment takes all non-zero elements in the first array and stores them in the second array.
4,22,4,11,11,4
|
Top
|