Question

Write a java method merge that accepts two arrays of integers and returns a new array...

Write a java method merge that accepts two arrays of integers and returns a new array containing all elements of the first array followed by all elements of the second.

int[] a1 = {12, 34, 56};

int[] a2 = {7, 8, 9, 10};  

int[] a3 = merge(a1, a2);

System.out.println(Arrays.toString(a3));

// [12, 34, 56, 7, 8, 9, 10]

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Main.java
import java.util.Arrays;
public class Main{

  public static int[] merge(int a[], int b[]){
    int c[] = new int[a.length+ b.length];
    int k = 0;
    for(int i = 0;i<a.length;i++){
      c[k++] = a[i];
    }
    for(int i = 0;i<b.length;i++){
      c[k++] = b[i];
    }
    return c;
  }


  public static void main(String[] args){
    int[] a1 = {12, 34, 56};

    int[] a2 = {7, 8, 9, 10};

    int[] a3 = merge(a1, a2);

    System.out.println(Arrays.toString(a3));

// [12, 34, 56, 7, 8, 9, 10]
  }
}

[12, 34, 56, 7, 8, 9, 10]

Add a comment
Know the answer?
Add Answer to:
Write a java method merge that accepts two arrays of integers and returns a new array...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

  • Write a method called alternate that accepts two Lists as its parameters and returns a new...

    Write a method called alternate that accepts two Lists as its parameters and returns a new List containing alternating elements from the two lists, in the following order: • First element from first list • First element from second list • Second element from first list • Second element from second list • Third element from first list • Third element from second list If the lists do not contain the same number of elements, the remaining elements from the...

  • Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...

    Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...

  • 4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array...

    4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array arr of integers argument returns a list of all permutations of these integers. (A permutation of a sequence of integers is a re-arrangement of the integers. For example, one permutation of 1, 3, 4, 8, 2 is 3, 1, 2, 8, 4.) For this problem, you may assume that the input array contains no duplicate entries. Your method should return an ArrayList of int...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • JAVA~ 1. Write a static method named countOdd(my_array) that returns the number of odd integers in...

    JAVA~ 1. Write a static method named countOdd(my_array) that returns the number of odd integers in a given array. If there are no odd numbers in the array, the method should return 0. If the array is empty, the method should also return 0. For example: Test Result System.out.println(countOdd(new int[]{2, 3, 5, 6})); 2 System.out.println(countOdd(new int[]{2})); 0 System.out.println(countOdd(new int[]{})); 0 System.out.println(countOdd(new int[]{-7, 2, 3, 8, 6, 6, 75, 38, 3, 2})); 4 public static int ----------------------------------------------------------------------------------------------------------- 2. Write a static...

  • ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...

    ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of integers (that the user inputs) as a parameter and returns a new array twice as large as the original, replacing every integer from the original array with a pair of integers, each half the original. If a number in the original array is odd, then the first number in the new pair should be one higher than the second so that the sum equals...

  • In java please: Write a method that accepts as a parameter a queue of integers that...

    In java please: Write a method that accepts as a parameter a queue of integers that are already sorted by absolute value, and modifies it so that the integers are sorted normally. Only use a single stack as auxiliary storage. For example, if a queue variable named q stores the following elements: front {1, -2, 4, 5, -7, -9, -12, 28, -34} back Then the call of reorder(q); should modify it to store the following values: front {-34, -12, -9,...

  • in Java Implement merging two sorted arrays into one sorted array. These arrays are sorted in...

    in Java Implement merging two sorted arrays into one sorted array. These arrays are sorted in descending order. For example [5, 4, 2]. Return an array with all the elements of these two arrays sorted, also in descending order. partb: if the two arrays as input are size n each. What is the big-O run time and space complexity of your implementation public int[] merge(int[] arr1, int[] arr2){

  • Write a program with a function named isEqualArr that accepts a pair of arrays of integers...

    Write a program with a function named isEqualArr that accepts a pair of arrays of integers as parameters and returns true if the arrays contain the same elements in the same order. If the arrays are not the same length, your function should return false. For example, if the following arrays are declared: int[] arr1 = {10, 20, 30, 40, 50, 60}; int[] arr2 = {10, 20, 30, 40, 50, 60}; int[] arr3 = {20, 3, 50, 10, 68}; The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT