Question

Write a method that takes two arrays of integers and return and array that is the union of both arrays. For instance, the fir
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Arrays;

public class ArrayMethods {

    // union method
    public static int[] union(int[] arr1, int[] arr2) {
        int[] result = new int[arr1.length + arr2.length];
        int index = 0;
        for (int i = 0; i < arr1.length; i++) {
            result[index++] = arr1[i];
        }
        for (int i = 0; i < arr2.length; i++) {
            result[index++] = arr2[i];
        }
        return result;
    }

    // reverse method
    public static int[] reverse(int[] arr) {
        int[] result = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            result[arr.length - i - 1] = arr[i];
        }
        return result;
    }

    // main method for testing. ignore it if not required
    public static void main(String[] args) {
        System.out.println(Arrays.toString(union(new int[]{1, 2, 3}, new int[]{4, 5, 6})));
        System.out.println(Arrays.toString(reverse(new int[]{1, 2, 3})));
    }
}

[1, 2, 3, 4, 5, 6] [3, 2, 1] Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Write a method that takes two arrays of integers and return and array that is the...
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
  • C programming Strictly - Write a program to sort an array of integers via arrays of...

    C programming Strictly - Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. Problem 1 (68 points): Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. The code should contain the following functions: 1)to randomly generate an array of integer numbers; 2) to allocate memory and build the arrays of pointers as shown in the figure...

  • 1. Write a static method named mode that takes an array of integers as a parameter...

    1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...

  • In C, write a method that takes in an array of integers and returns its size....

    In C, write a method that takes in an array of integers and returns its size. Q9]Write a method that takes in an array of integers and returns it's sizel10 points

  • java code Write a method called reverse that takes an array of integers as an input...

    java code Write a method called reverse that takes an array of integers as an input and returns the same values in reverse order as the output. Test your method in the main.

  • The method generate() will produce integer arrays of a random size between 10 and 20 members. ...

    The method generate() will produce integer arrays of a random size between 10 and 20 members. The method print() will print out the members of an integer array input. The method insert() will accept two arrays as inputs. It will produce a new array long enough to contain both arrays, and both input arrays should be inserted into the new array in the following manner: select a random member of the first array, and insert every member of the second...

  • Write a program which takes 2 arrays of 10 integers each, a and b. c is...

    Write a program which takes 2 arrays of 10 integers each, a and b. c is another array with 20 integers. The program should put into c the appending of b to a, the first 10 integers of c from array a, the latter 10 from b. Then the program should display the array c, then find and display the Minimum value of the array c.

  • Write a method that takes in a two-dimensional array of integers, do some calculation for each...

    Write a method that takes in a two-dimensional array of integers, do some calculation for each row in the two-dimensional array, and returns a one-dimensional array of sums for each row. Your code must work any array dimensions, including jagged arrays. The calculations are: • Sum of each row • Maximum value of each row • Minimum value of each row • Average of each row Here is an example an array passed and returns an array of sums for...

  • Write a C function that takes two strings (character arrays) as input and return 1 if...

    Write a C function that takes two strings (character arrays) as input and return 1 if they are equal and return 0 if they re not. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal in the same order.

  • Write a C function that takes two strings (character arrays) as input and return 1 if...

    Write a C function that takes two strings (character arrays) as input and return 1 if they are equal and return 0 if they re not. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal in the same order.

  • USING C++ and PYTHON Please Help me: 1. Write a function that takes in an array...

    USING C++ and PYTHON Please Help me: 1. Write a function that takes in an array of integers (and the size of the array in the C/C++ version). The function will reverse the array of integers and return the reversed array. Print the array that is returned from the function. How you "return" the array is up to you, but do not overwrite the original array. Note: in Python, the term list is more appropriate, see https://docs.python.org/3.5/tutorial/datastructures.html a) Example input:...

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
ADVERTISEMENT