Question

USING JAVA ONLY Merge two 1D arrays into a single array. take one value from each...

USING JAVA ONLY

Merge two 1D arrays into a single array. take one value from each array at a time.

Sample:

array 1: 1,2,3,4,5

array 2: 2,4

new array: 1,2,2,4,3,4,5

Homework Answers

Answer #1
import java.util.Scanner;

public class MergeArrays {

    public static int[] merge(int[] a, int[] b) {
        int[] result = new int[a.length + b.length];
        int maxLength = a.length, index = 0;
        if (b.length > maxLength) maxLength = b.length;
        for (int i = 0; i < maxLength; i++) {
            if (i < a.length) {
                result[index++] = a[i];
            }
            if (i < b.length) {
                result[index++] = b[i];
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter size of first array: ");
        int n1 = in.nextInt();
        int[] arr1 = new int[n1];
        System.out.print("Enter " + n1 + " numbers: ");
        for (int i = 0; i < n1; ++i) {
            arr1[i] = in.nextInt();
        }
        System.out.print("Enter size of second array: ");
        int n2 = in.nextInt();
        int[] arr2 = new int[n2];
        System.out.print("Enter " + n2 + " numbers: ");
        for (int i = 0; i < n2; ++i) {
            arr2[i] = in.nextInt();
        }

        int[] arr = merge(arr1, arr2);
        System.out.print("new array: ");
        for (int i = 0; i < arr.length; ++i) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
USING JAVA ONLY - THE NEW ARRAY IS NOT JUST THE VALUES IN INCREASING ORDER Merge...
USING JAVA ONLY - THE NEW ARRAY IS NOT JUST THE VALUES IN INCREASING ORDER Merge two 1D arrays into a single array. take one value from each array at a time. The new array takes a value from array 1, than one from array two, and keeps repeating until all values are in the new array. Sample: array 1: 1,2,3,4,5 array 2: 2,4 new array: 1,2,2,4,3,4,5 Sample 2: array 1: is 8,2,4,7,2,6,2 array 2: 4,7,3 new array: 8,4,2,7,4,3,7,2,6,2
IN JAVA PLEASE This problem requires you to code the Merge part of the Merge Sort...
IN JAVA PLEASE This problem requires you to code the Merge part of the Merge Sort algorithm. The Merge step merges n elements which takes O(n) time. Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Any code that is found to exceed linear time will fail the tests. Example 1: Input: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6]
java Write a single Java statements to accomplish each of the following: a) Displaythevalueoftheseventhelementofcharacterarraych. b) Considering...
java Write a single Java statements to accomplish each of the following: a) Displaythevalueoftheseventhelementofcharacterarraych. b) Considering “Scanner in = new Scanner (System.in);”, input a value into element 5 of one-dimensional double array nums. c) Initialize each of the four elements of the one-dimensional integer array test to 7. d) Declare and create an array called table as a float array that has four rows and three columns. e) Considering the following ArrayList declaration, insert “test” at the fourth position (index...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
Create a class called BirthYear. It should only have two member, both of them arrays. One...
Create a class called BirthYear. It should only have two member, both of them arrays. One of type string called Names. The second of type int called BirthYears. in your main class create a StudentBirthYear object. Make sure both arrays are of the size 10. Add ten different names and ten different birth years. Then display each name with its birth year using only 1 for loop. (You can display in a for loop as well as a foreach loop,...
Using Java write all 4 methods in one class 1) Write a value-returning method that returns...
Using Java write all 4 methods in one class 1) Write a value-returning method that returns the number of elements in an integer array. 2) Write a void method that multiples by 2 all the elements in an array of float. 3) Write a value- returning method that returns the product of all elements in an integer array. 4) Write a method that returns the total # of elements greater or equal to 90 in an array of integers.
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers and sort it in-place. Write a program that generates random integer arrays (hint: use seed appropriately to avoid generating same sequences) of lengths 10, 100, 1000, 10,000, 100,000, 1000,000, and then sorts each using each of the sorting functions from (a), and measures the time in nanoseconds. The program will repeat this process 30 times and will compute the average execution time for each...
Please write a java program Instructions Your goal is to take N integer inputs from the...
Please write a java program Instructions Your goal is to take N integer inputs from the user -- N's value will be given by the user as well. You can assume the user provides a valid value for N, i.e., >0. Store the input integers in an array of size N in the order they are provided. These tasks should be done in the main() method. Create a new method called checkArray() that will take the previously created array as...