Inside Apr25InsertMiddleOfArray.java
public String[] createNewArrayWithAdditional(String[] original, int index, String word) { /** * String[] original = new String[]{"a", "c", "d", "e"}; * String[] newArray = createNewArrayWithAdditional(original, 3, "z"); * * newArray = a,c,d,e,z * original = a,c,d,e */ String[] result = new String[original.length+1]; int originalIndex = 0; int newIndex = 0; while(originalIndex < original.length) { /** * 1. copy each element from the original to the result one element every loop. * 2. when originalIndex == index, insert word into result array. * 3. everytime it loops, you need to increment originalIndex and newIndex. * Except when originalIndex == index. */ } return result; }
public static String[] copyAndDeleteInTheMiddle(String[] arr, int index) { // Write a method that returns a new array that contains all the elements of the input // array arr, except for the element at the specified index, which is removed. // The elements after the specified index are shifted to fill the gap. // Example: // String[] letters = {"a", "b", "c", "d", "e"}; // String[] newArr = removeAtIndex(letters, 2); // newArr = ["a", "b", "d", "e"] }