logo Practice-It logo


Language/Type: Java arrays
Author: Marty Stepp (on 2020/04/16)

Write a static method called splice that takes as parameters an array of integers and a "from index" (inclusive) and "to index" (exclusive) and that returns a new array containing the result of splicing together three segments of the array. The from and to indexes specify a sublist. For example, if list stores the following:

            [7, 5, 8, 5, 9, 7, 2, 3]

then the call splice(list, 2, 4) indicates that the array should be rearranged using the sublist from 2 to 4:

            [7, 5]       [8, 5]    [9, 7, 2, 3]
        before sublist   sublist   after sublist

The new array should contain the values after the sublist followed by the values in the sublist followed by the values before the sublist. For this example, it would return [9, 7, 2, 3, 8, 5, 7, 5].

You may assume that the indexes passed as parameters specify a legal sublist of the list, although it might be empty. The method should not construct any extra data structures other than the array to be returned and it should not alter its array parameter. You are not allowed to call methods of the Arrays class to solve this problem.

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.