1

i am writing a program and have a problem with delete an array of object, its a logic error not compiler take a look in the function please:

public void deleteMovie(movies []a,String mID,int n) {
    int i;
    boolean check=true;
    for (i=0;i<n;i++) {
        if (a[i].MovieID.equals(mID)) {

            while (i<n-1) {
                a[i]=a[i+1];
                i++;
            }
            n--;
        } else check = false;
    }

    if(check == false)
        System.out.println("unfound Element ");
}

when i tried it even n does not decrement any suggestions?

2
  • can you update your question with code of movie class? Commented Nov 13, 2020 at 13:00
  • I would suggest using a mutable collection, like an ArrayList for this kind of stuff. With this, you won't have to deal with the troubles of deleting from an array, as this functionality is given to you for free when using these collections. Alternatives (like suggested) can be found here. As for your case, When you say n does not decrement, you probably have another issue going on. Commented Nov 13, 2020 at 13:01

2 Answers 2

0

Since everyone already told you about Java Collections I keeped it simple and hold onto the Array in case you did not like ArrayLists maybe.

public movies[] deleteMovie(movies[] a, String mID) {
    boolean check = false;
    int j = 0;
    movies[] b = new String[a.length - 1];
    for (int i = 0; i < a.length; i++) {
        if(!a[i].MovieID.equals(mID)) {
            b[j] = a[i];
            j++;
        } else check = true;
    }

    if(check) {
        System.out.println("found Element ");
    }

    return b;
}
Sign up to request clarification or add additional context in comments.

1 Comment

movies[] b = new String[a.length - 1]; :-)
0

array is not data structure when you plan to remove an items, because to do so you have to create another array and copy all other items. Check this out:

static class Movie {

    private final String id;

    public Movie(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

}

public static Movie[] deleteMovieById(Movie[] movies, String id) {
    int pos = findMovieById(movies, id);

    if (pos == -1)
        return movies;  // not found; retrieve source array

    Movie[] arr = new Movie[movies.length - 1];
    System.arraycopy(movies, 0, arr, 0, pos);
    System.arraycopy(movies, pos + 1, arr, pos, arr.length - pos);
    return arr;
}

private static int findMovieById(Movie[] movies, String id) {
    for (int i = 0; i < movies.length; i++)
        if (movies[i].getId().equals(id))
            return i;

    return -1;
}

Output:

Movie[] movies = {
        new Movie("1"),
        new Movie("2"),
        new Movie("3") };
Movie[] res = deleteMovieById(movies, "1");

Correct data structure to hold and to remove with constant time element is Map:

public static void deleteMovieById(Map<String, Movie> movies, String id) {
    movies.remove(id);
}

Output:

Map<String, Movie> movies = Map.of(
        "1", new Movie("1"),
        "2", new Movie("2"),
        "3", new Movie("3"));

deleteMovieById(movies, "1");

Can you see the difference?!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.