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?

nkrivenko
  • 1,231
  • 3
  • 14
  • 23
fei alsh
  • 21
  • 3
  • can you update your question with code of movie class? – Dhruvam Gupta Nov 13 '20 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](https://stackoverflow.com/questions/22718898/deleting-an-object-from-an-array-java). As for your case, When you say _n does not decrement_, you probably have another issue going on. – maloomeister Nov 13 '20 at 13:01

2 Answers2

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;
}
nkrivenko
  • 1,231
  • 3
  • 14
  • 23
Erinas
  • 45
  • 9
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?!

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35