Knowee
Questions
Features
Study Tools

Copy the skeleton of code below into your answer.  Then in the space indicated, add your own code to write a method that takes two arrays  and returns a new array the LENGTH OF THE SHORTER of the two where at each position if the two elements don't match there is a 0 and if the two elements do match there is a copy of that value.  The two original arrays should NOT be changed by this method.For example, if the calling method makes the callint[] array1 = {2, 4, 5, 7, 3, 5, 7, 88};int[] array2 = {8, 4, 3, 0, 3};int[] result = matches(array1,array2);After the call finishes, array and result will have the values:result = {0, 4, 0, 0, 3}array1 = {2, 4, 5, 7, 3, 5, 7, 88}array2 = {8, 4, 3, 0, 3}As another example, if the calling method makes the callint[] array1 = {8, 3, 7};int[] array2 = {8, 9, 7, 9, 4};int[] result = matches(array1,array2);After the call finishes, array and result will have the values:result = {8, 0, 7}array1 = {8, 3, 7}array2 = {8, 9, 7, 9, 4} If the method is called with any 0-length array it should return a new 0-length array:int[] array1 = {2, 4};int[] array2 = {};int[] result = matches(array1, array2);After the above call finishes, array and result will have the values:result = {}array1 = {2, 4}array2 = {}Use the skeleton of the method below to write your code.public static int[] matches(int[] arr1, int[] arr2) {   // TODO: Replace with your code}

Question

Copy the skeleton of code below into your answer.  Then in the space indicated, add your own code to write a method that takes two arrays  and returns a new array the LENGTH OF THE SHORTER of the two where at each position if the two elements don't match there is a 0 and if the two elements do match there is a copy of that value.  The two original arrays should NOT be changed by this method.For example, if the calling method makes the callint[] array1 = {2, 4, 5, 7, 3, 5, 7, 88};int[] array2 = {8, 4, 3, 0, 3};int[] result = matches(array1,array2);After the call finishes, array and result will have the values:result = {0, 4, 0, 0, 3}array1 = {2, 4, 5, 7, 3, 5, 7, 88}array2 = {8, 4, 3, 0, 3}As another example, if the calling method makes the callint[] array1 = {8, 3, 7};int[] array2 = {8, 9, 7, 9, 4};int[] result = matches(array1,array2);After the call finishes, array and result will have the values:result = {8, 0, 7}array1 = {8, 3, 7}array2 = {8, 9, 7, 9, 4} If the method is called with any 0-length array it should return a new 0-length array:int[] array1 = {2, 4};int[] array2 = {};int[] result = matches(array1, array2);After the above call finishes, array and result will have the values:result = {}array1 = {2, 4}array2 = {}Use the skeleton of the method below to write your code.public static int[] matches(int[] arr1, int[] arr2) {   // TODO: Replace with your code}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is the code that fulfills the requirements:

public static int[] matches(int[] arr1, int[] arr2) {
    // Determine the length of the shorter array
    int length = Math.min(arr1.length, arr2.length);

    // Create a new array with the length of the shorter array
    int[] result = new int[length];

    // Iterate over the elements of the shorter array
    for (int i = 0; i < length; i++) {
        // If the elements at the current position in both arrays match
        if (arr1[i] == arr2[i]) {
            // Copy the value to the result array
            result[i] = arr1[i];
        } else {
            // Otherwise, set the value at the current position in the result array to 0
            result[i] = 0;
        }
    }

    // Return the result array
    return result;
}

This code first determines the length of the shorter array and creates a new array with that length. It then iterates over the elements of the shorter array. If the elements at the current position in both arrays match, it copies the value to the result array. Otherwise, it sets the value at the current position in the result array to 0. Finally, it returns the result array.

This problem has been solved

Similar Questions

Write a Java program to remove the duplicate elements of a given array and return the new length of the array.

Write a function bool equals(int a[], int a_size, int b[], int b_size) that checks whether two arrays have the same elements in the same order.

int [ ] array = { 2, 4, 6, 8, 1, 3, 5, 7};        int george = 0;        for (int i = 0; i < array.length; i++)              george += array[i];        System.out.print(george);

You are given two arrays of integers a1,a2,…,an and b1,b2,…,bn. Before applying any operations, you can reorder the elements of each array as you wish. Then, in one operation, you will perform both of the following actions, if the arrays are not empty:1. Choose any element from array a and remove it (all remaining elements are shifted to a new array a)2. Choose any element from array b and remove it (all remaining elements are shifted to a new array b)Let k be the final size of both arrays. You need to find the minimum number of operations required to satisfy aiInput Format2 (size of arrays a & b)1 1 (array a)3 2 (array b)Constraintsall inputs are positive integersOutput Format0 (number of minimum operations)Sample Input 041 5 1 53 8 3 3Sample Output 01

You are given two arrays of equal length, nums1 and nums2.Each element in nums1 has been increased (or decreased in the case of negative) by an integer, represented by the variable x.As a result, nums1 becomes equal to nums2. Two arrays are considered equal when they contain the same integers with the same frequencies.Return the integer x. Example 1:Input: nums1 = [2,6,4], nums2 = [9,7,5]Output: 3Explanation:The integer added to each element of nums1 is 3.Example 2:Input: nums1 = [10], nums2 = [5]Output: -5Explanation:The integer added to each element of nums1 is -5.Example 3:Input: nums1 = [1,1,1,1], nums2 = [1,1,1,1]Output: 0Explanation:The integer added to each element of nums1 is 0. Constraints:1 <= nums1.length == nums2.length <= 1000 <= nums1[i], nums2[i] <= 1000The test cases are generated in a way that there is an integer x such that nums1 can become equal to nums2 by adding x to each element of nums1.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.