Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Once an array is created, its size is fixed. Occasionally, you need to add more

ID: 3577113 • Letter: O

Question

Once an array is created, its size is fixed. Occasionally, you need to add more values to an array, but it is full. In such a case, you can create a new larger array to replace the existing array. Write a method with the following header: public static int[] doubleCapacity (int[] list) The method returns an array variable referencing to a new array that doubles the size of the input array referenced by the parameter list. The contents of the first half of the new array should be the same as the input array; use 0 as the value for the elements in the second half of the new array. For example, if the input array is {1, 2, 3, 4, 5}, the new array should be {1, 2, 3, 4, 5, 0, 0, 0, 0, 0}. Put the doubleCapacity method and the main method in one class. The purpose of the main method is to test your doubleCapacity method. Design your own main method—the minimal requirements are: 1. Create an input array of your choice. 2. Display the contents of the input array. 3. Invoke the doubleCapacity method to create a new double-sized array. 4. Display the contents of the new array.

Explanation / Answer

DoubleArrayTest.java


import java.util.Arrays;

public class DoubleArrayTest {

  
   public static void main(String[] args) {
       java.util.Scanner in = new java.util.Scanner(System.in);
       int n=5;
       int a[] = new int[n];
       for(int i=0; i<n; i++){
       System.out.println("Please Enter Integer value: ");
       a[i] = in.nextInt();
       }
       System.out.println("Array Size ["+a.length+"]");
       System.out.println(Arrays.toString(a));
       int b[] = doubleCapacity(a);
       displayArrayElements(a,b);
   }
   public static int[] doubleCapacity (int[] list){
       int b[] = new int[2 * list.length];
       java.util.Scanner in = new java.util.Scanner(System.in);
       for(int i=0; i<list.length; i++){
       b[i] = list[i];
       }
       return b;
   }
   public static void displayArrayElements (int[] list1, int[] list2){
       System.out.println("First Array Size ["+list1.length+"]");
       for(int i=0; i<list1.length; i++){
           System.out.println("First Array Element ["+i+"] : "+list1[i]);
       }
       System.out.println("Second Array Size ["+list2.length+"]");
       for(int i=0; i<list2.length; i++){
           System.out.println("Second Array Element ["+i+"] : "+list2[i]);
       }      
   }

}

Output:

Please Enter Integer value:
1
Please Enter Integer value:
2
Please Enter Integer value:
3
Please Enter Integer value:
4
Please Enter Integer value:
5
Array Size [5]
[1, 2, 3, 4, 5]
First Array Size [5]
First Array Element [0] : 1
First Array Element [1] : 2
First Array Element [2] : 3
First Array Element [3] : 4
First Array Element [4] : 5
Second Array Size [10]
Second Array Element [0] : 1
Second Array Element [1] : 2
Second Array Element [2] : 3
Second Array Element [3] : 4
Second Array Element [4] : 5
Second Array Element [5] : 0
Second Array Element [6] : 0
Second Array Element [7] : 0
Second Array Element [8] : 0
Second Array Element [9] : 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote