Write a method not a complete program only a method called d

Write a method (not a complete program, only a method) called doubleSize that accepts an integer array as a parameter and returns a reference to a new integer array that is twice as long and contains all of the elements of the first array in the same positions.

For example if the array contains the following integers:


The method doubleSize will return:

6 7   3   5   2  

Solution

import java.util.Arrays;

public class DoubleArray {
   /**
   * @param args
   */
   public static void main(String[] args) {

       int[] arr = { 6, 7, 3, 5, 2 };
       int[] doubleArr = doubleSize(arr);
       System.out.println(\"Double sized :\" + Arrays.toString(doubleArr));
   }

   /**
   * @param arr
   * @return
   */
   public static int[] doubleSize(int[] arr) {

       int[] resultArr = new int[arr.length * 2];

       for (int i = 0; i < arr.length; i++) {

           resultArr[i] = arr[i];
       }

       return resultArr;

   }
}

OUTPUT:

Double sized :[6, 7, 3, 5, 2, 0, 0, 0, 0, 0]

Write a method (not a complete program, only a method) called doubleSize that accepts an integer array as a parameter and returns a reference to a new integer a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site