The StaticArray class provides methods to manipulate a static integer array: insertEnd adds an integer to the end, removeEnd removes from the end, insertMiddle inserts in the middle by shifting elements, and removeMiddle removes in the middle by shifting elements left. It also includes a printArr method to print the array.
The StaticArray class provides methods to manipulate a static integer array: insertEnd adds an integer to the end, removeEnd removes from the end, insertMiddle inserts in the middle by shifting elements, and removeMiddle removes in the middle by shifting elements left. It also includes a printArr method to print the array.
The StaticArray class provides methods to manipulate a static integer array: insertEnd adds an integer to the end, removeEnd removes from the end, insertMiddle inserts in the middle by shifting elements, and removeMiddle removes in the middle by shifting elements left. It also includes a printArr method to print the array.
// Length is the number of 'real' values in arr, and capacity // is the size (aka memory allocated for the fixed size array). public void insertEnd(int[] arr, int n, int length, int capacity) { if (length < capacity) { arr[length] = n; } }
// Remove from the last position in the array if the array
// is not empty (i.e. length is non-zero). public void removeEnd(int[] arr, int length) { if (length > 0) { // Overwrite last element with some default value. // We would also the length to decreased by 1. arr[length - 1] = 0; length--; } }
// Insert n into index i after shifting elements to the right.
// Assuming i is a valid index and arr is not full. public void insertMiddle(int[] arr, int i, int n, int length) { // Shift starting from the end to i. for (int index = length - 1; index > i - 1; index--) { arr[index + 1] = arr[index]; } //Insert at i arr[i] = n; }
// Remove value at index i before shifting elements to the left.
// Assuming i is a valid index. public void removeMiddle(int[] arr, int i, int length) { // Shift starting from i + 1 to end. for (int index = i + 1; index < length; index++) { arr[index - 1] = arr[index]; } // No need to 'remove' arr[i], since we already shifted }
public void printArr(int[] arr, int length) {
for (int i = 0; i < length; i++) { System.out.print(arr[i] + " "); } System.out.println(); } }