In: Computer Science
Note that swapArrayEnds() does not need to return the result, as it makes changes to the array referenced by the parameter, which is the same array reference by the argument. On the other hand, removeTen() does not direct make changes to the array, but creates a new array, which requires to be made.
public class RemoveTen {
public static void main(String[] args) {
int[] nums = {1, 10, 10, 2};
swapArrayEnds(nums);
for(int i = 0; i < nums.length; i++)
System.out.print(nums[i] + " ");
int[] result = removeTen(nums);
for(int i = 0; i < result.length; i++)
System.out.print(result[i] + " ");
System.out.println();
for(int i = 0; i < nums.length; i++)
System.out.print(nums[i] + " ");
System.out.println();
}
public static void swapArrayEnds(int[] nums) {
/*FIXME Complete the implementation of the swapArrayEnds
method*/
}
public static int[] removeTen(int[] nums) {
/*FIXME Complete the implementation of the removeTen
method*/
}
}
public class RemoveTen {
public static void main(String[] args) {
int[] nums = {1, 10, 10, 2};
swapArrayEnds(nums);
for(int i = 0; i < nums.length; i++)
System.out.print(nums[i] + " ");
System.out.println();
int[] result = removeTen(nums);
for(int i = 0; i < result.length; i++)
System.out.print(result[i] + " ");
System.out.println();
for(int i = 0; i < nums.length; i++)
System.out.print(nums[i] + " ");
System.out.println();
}
public static void swapArrayEnds(int[] nums) {
int temp=nums[0];
nums[0]=nums[nums.length-1];
nums[nums.length-1]=temp;
}
public static int[] removeTen(int[] nums) {
int new_array[]=new int[nums.length];
int k=0;
for(int i=0;i<nums.length;i++)
{
if(nums[i]!=10)
new_array[k++]=nums[i];
}
if(k<nums.length)
for(int i=k;i<nums.length;i++)
new_array[i]=0;
return new_array;
}
}