"Working code Example : "(Execution)
public class DeleteValue {
String fruits[] = { "apple", "orange", "banana", "mango", "Cherries", "Blueberries" }; // array of fruits
public void delete(String fruitName) {
// printing array of fruits before deletion
System.out.println("\nAvailable fruits Before delete : " + fruits.length + "\n");
for (String s : fruits) {
System.out.println(s + " is Available\n");
}
int length = fruits.length;
int lengthNew = length;
int countNull = 0;
// 1. Find and delete the fruit
for (int i = 0; i < fruits.length; i++) {
if (fruits[i] == fruitName) {
fruits[i] = null;
break;
}
}
// count the null or deleted values so that we can create a new array of length
// minus the deleted fruit
for (int i = 0; i < fruits.length; i++) {
if (fruits[i] == null) {
countNull++;
}
}
// new array length
lengthNew = lengthNew - countNull;
// create new array of fruits
String newFruits[] = new String[lengthNew];
// assigning values from original array to new
int j = 0;
for (int i = 0; i < fruits.length; i++) {
if (fruits[i] == null) {
continue;
}
if (fruits[i] != null) {
newFruits[j] = fruits[i];
j++;
}
}
System.out.println("------------------------------------------");
System.out.println("\nAvailable fruits after delete : " + newFruits.length + "\n");
// print the final output
for (String s : newFruits) {
System.out.println(s + " is Available\n");
}
}
public static void main(String args[]) {
new DeleteValue().delete("mango");
;
}
}
Explanation :
The only Issue I am having its that Fruit Array is not decreasing the
size
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed
So what we can do is either use a dynamic array or we can use a work around like the above code:
If you want to "grow" or "shrink" an existing array, you have to allocate a new array of the appropriate size and copy the array elements from old array to the new array.
In the above code I have provided comments for the working steps.
We are solving this problem is three steps:
- Find and delete the fruit item from array.
- count the deleted values from old array so that we can create a new array
of that size.
- Move all remaining items from old array to new array.