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
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...
Write a Java class called Grades in a class file called Grades.java. 2. Grades reads from...
Write a Java class called Grades in a class file called Grades.java. 2. Grades reads from a text file containing a series of course grades (a value between 0.0 and 100.0) with one grade entry per line. However, the first line in the file is an integer value specifying how many grade entries are contained in the file. 3. The Grades class contains four static methods: a. A method called loadGrades() that opens the file, reads in the data and...
Complete the java code as per the comments public class Sorting {    ///////////////////////////////////////////////    //...
Complete the java code as per the comments public class Sorting {    ///////////////////////////////////////////////    // STEP 1 -- Make sorting methods generic    ///////////////////////////////////////////////       /**    * Re-orders the contents given array using the insertion sort algorithm.    *    * @param data The array to be sorted.    */    //TODO: Make me generic to work on any kind of Comparable data!    public static void insertionSort(int[] data)    {        int insert; // temporary...