-4

here is my code and I need to remove a fruit from the array

public void delete(String fruitName) {
    for (int i = 0; i < fruits.length; i++) {

        if ( fruits[i].equals(fruitName)) {
            fruits[i] = fruits[i+1];
            break;
        }
    }

// ["banana", "apple, "Mango"] if removed banana then output ["apple", "Mango"].

    // TODO: 1. implement this method.
    /* TODO: 2. you may need to consult Java API documentation for the String class. 
     *          Write a comment in the code, the method of the String class you
     *          look up and the URL to the documentation the method 
     */
}
  • 3
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ] and [ask]. – Federico klez Culloca Feb 27 '18 at 11:48
  • Also `if ( fruits[i] == fruitName)` this won't work – Federico klez Culloca Feb 27 '18 at 11:50
  • 1
    look at yor TODO list in the comments. what they mean is this: https://stackoverflow.com/a/513839/3959856 – Jack Flamp Feb 27 '18 at 11:51
  • No, sir I dont want nobody to write any code for me . This is the code I have written myself . The only Issue I am having its that Fruit Array is not decreasing the size . – sunny khan Feb 27 '18 at 11:58
  • Why should it? You're not shrinking it. – Federico klez Culloca Feb 27 '18 at 13:05

1 Answers1

0

"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:

  1. Find and delete the fruit item from array.
  2. count the deleted values from old array so that we can create a new array of that size.
  3. Move all remaining items from old array to new array.
Abhi
  • 995
  • 1
  • 8
  • 12