Menu

Given an oversize array with size1 elements and a second oversize array with size2 elements, please help me write method that returns the first array with the elements of the second appended to the en…

Question

Answered step-by-step

Given an oversize array with size1 elements and a second oversize…

Given an oversize array with size1 elements and a second oversize array with size2 elements, please help me write method that returns the first array with the elements of the second appended to the end. If the capacity of the oversize array is not large enough to append all of the elements, append as many as will fit.

Hint: Do not construct a new array. Instead, modify the contents of the oversize array inside the method.

 

 

import java.util.Arrays;
import java.util.Scanner;

public class AppendOversize 
{

public static void main(String[] args) 
{
 // Do not edit this method
 Scanner keyboard = new Scanner(System.in);
 final int SIZE = 20;
 int[] array = new int[SIZE];
 int[] append = new int[SIZE];
 
 // Read in the first array
 int value = keyboard.nextInt(); // priming read
 int index;
 for(index=0; index<array.length && value != -1; ++index)
 {
  array[index] = value;
  value = keyboard.nextInt(); // priming read
 }
 keyboard.nextLine(); // get the carriage return out of there
 
 // Get the size from the number of values put in the first array
 int arraySize = index;
 
 // Read in the second array
 value = keyboard.nextInt(); // priming read
 for (index=0; index<append.length && value != -1; ++index)
 {
  append[index] = value;
  value = keyboard.nextInt(); // priming read
 }
 int appendSize = index;
 
 arraySize = appendOversize(array, arraySize, append, appendSize);
 System.out.println(“Array is ” + Arrays.toString(array) + ” size is ” + arraySize);
 keyboard.close();
}

public static int appendOversize(int[] array, int size, int[] toAppend, int toAppendSize) 
{
 
 // Put your code here

   
   return 0;  // edit this line

}
 

OR

Leave a Reply

Your email address will not be published. Required fields are marked *