Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

zeroOut

Language/Type: Java arrays
Author: Brad Goring

Write a static method named zeroOut that accepts two arrays of integers a1 and a2 as parameters and replaces any occurrences of a2 in a1 with zeroes. The sequence of elements in a2 may appear anywhere in a1 but must appear consecutively and in the same order. For example, if variables called a1 and a2 store the following values:

int[] a1 = {1, 2, 3, 4, 1, 2, 3, 4, 5};
int[] a2 = {2, 3, 4};

The call of zeroOut(a1, a2); should modify a1's contents to be {1, 0, 0, 0, 1, 0, 0, 0, 5}. Note that the pattern can occur many times, even consecutively. For the following two arrays a3 and a4:

int[] a3 = {5, 5, 5, 18, 5, 42, 5, 5, 5, 5};
int[] a4 = {5, 5};

The call of zeroOut(a3, a4); should modify a3's contents to be {0, 0, 5, 18, 5, 42, 0, 0, 0, 0}.

You may assume that both arrays passed to your method will have lengths of at least 1. If a2 is not found in a1, or if a1's length is shorter than a2's, then a1 is not modified by the call to your method. Please note that a1's contents are being modified in place; you are not supposed to return a new array. Do not modify the contents of a2.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.