Question

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

Homework Answers

Answer #1
import java.util.Scanner;

public class MergeIntArrays {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("array 1: ");
        String[] s1 = scanner.nextLine().split(",");
        int arr1[] = new int[s1.length];
        for(int i = 0;i<arr1.length;i++){
            arr1[i] = Integer.parseInt(s1[i]);
        }

        System.out.print("array 2: ");
        String[] s2 = scanner.nextLine().split(",");
        int arr2[] = new int[s2.length];
        for(int i = 0;i<arr2.length;i++){
            arr2[i] = Integer.parseInt(s2[i]);
        }

        int arr[] = new int[arr1.length+arr2.length];
        int i = 0, k =0;
        while(i<arr1.length && i<arr2.length){
            arr[k++] = arr1[i];
            arr[k++] = arr2[i];
            i++;
        }

        while(i<arr1.length){
            arr[k++] = arr1[i++];
        }

        while(i<arr2.length){
            arr[k++] = arr2[i++];
        }

        System.out.print("new array: ");
        for(int j = 0;j<arr.length;j++){
            if(j == 0){
                System.out.print(arr[j]);
            }
            else{
                System.out.print(","+arr[j]);
            }
        }
        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 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
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...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
Write a java program that creates an integer array with 50 random values, prompts the user...
Write a java program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. “Out of Bounds”) and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle invalid inputs,...
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...
Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1,...
Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9]) add it with 2nd item...
How to use C++ figure this question? Collection and Sorted Collection An array is great for...
How to use C++ figure this question? Collection and Sorted Collection An array is great for storing a list of values. However, one challenge when working with arrays is they have a fixed size. If you declare an array of size 10 and later need to actually store 11 elements you have to create a new array and copy everything over. C++ (and other langauges) create classes that handle this and other operations behind the scenes. In this assignment, we...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT