In: Computer Science
THIS IS FOR JAVA
Given an oversize array of size words and a word to remove, write a method that returns the array with each occurrence of the given word removed.
Shift the remaining words in the nonempty part of the array to the left so that each occurrence of the given word is overwritten. (Leave the words in the empty part of the array unchanged.)
Hint: To understand the test cases, note that the size (but not the capacity) of the array shrinks if removeWord appears in the array. In the first test case, for example, "up" is removed and "down" and "left" shift to the left by one element. The size of the array shrinks from 3 to 2, although the capacity is still 3. The third element is now in the empty part of the array. This element is equal to "left", but it is treated as empty space.
here is the code:
import java.util.Arrays; | |
import java.util.Scanner; | |
public class RemoveOversize | |
{ | |
public static void main(String[] args) | |
{ | |
// Do not edit this code | |
Scanner keyboard = new Scanner(System.in); | |
final int SIZE = 20; // for the oversized array | |
String[] array = new String[SIZE]; | |
// Read in data from keyboard--all array elements are presumed to be on one line | |
String contents = keyboard.nextLine(); | |
String[] contentsInArray = contents.split(" "); | |
// Make sure we're in the range of both arrays | |
int index; | |
for(index = 0; index <contentsInArray.length && index < array.length; ++index) | |
{ | |
array[index] = contentsInArray[index]; | |
} | |
int arraySize = index; | |
String value = keyboard.nextLine(); // Get the value to remove | |
int resultSize = removeOversize(array, arraySize, value); | |
System.out.println("The resulting array contains " + Arrays.toString(array) + " and is of size " + resultSize); | |
keyboard.close(); | |
} | |
public static int removeOversize(String[] source, int sourceSize, String removeMe) | |
{ | |
// Write the code below. | |
return null; | |
} | |
} |
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: I only completed removeOversize method as requested. Did not touch main method. The output will have null values for empty spaces. This is normal (not any mistake by me), because inside the main method, using Arrays.toString(), you are printing the entire array and not just the filled values. If you need to return an array of exact size, you will need to change the signature of removeOversize method. Right now it is returning the new size of the array, not entirely the new array.
// RemoveOversize.java
import java.util.Arrays;
import java.util.Scanner;
public class RemoveOversize {
public static void main(String[] args) {
// Do not edit this code
Scanner keyboard = new Scanner(System.in);
final int SIZE = 20; // for the oversized array
String[] array = new String[SIZE];
// Read in data from keyboard--all array elements are presumed to be on
// one line
String contents = keyboard.nextLine();
String[] contentsInArray = contents.split(" ");
// Make sure we're in the range of both arrays
int index;
for (index = 0; index < contentsInArray.length && index < array.length; ++index) {
array[index] = contentsInArray[index];
}
int arraySize = index;
String value = keyboard.nextLine(); // Get the value to remove
int resultSize = removeOversize(array, arraySize, value);
System.out.println("The resulting array contains "
+ Arrays.toString(array) + " and is of size " + resultSize);
keyboard.close();
}
public static int removeOversize(String[] source, int sourceSize,
String removeMe) {
// looping from last to front (easier to remove)
for (int i = sourceSize - 1; i >= 0; i--) {
// checking if value at i needs to be removed
if (source[i].equals(removeMe)) {
// looping and shifting every elements on the right to one place
// to the left
for (int j = i; j < sourceSize - 1; j++) {
source[j] = source[j + 1];
}
// making the last element null
source[sourceSize - 1] = null;
// updating size
sourceSize--;
}
}
// returning new size
return sourceSize;
}
}
/*OUTPUT (user input in green)*/
hello abc hi how abc hello abc this is fun
abc
The resulting array contains [hello, hi, how, hello, this, is, fun, null, null, null, null, null, null, null, null, null, null, null, null, null] and is of size 7