简体   繁体   中英

method to remove object if condition

public void removeThem(int nrF){
    if(index == 0){
        System.out.println("array is empty!");
        return;
    }
    for(int i = 0; i < index; i++){
        String e = books[i].getName();
        if(e.length() > 12 && e.split(" ").length > nrF){
            for(int j = i; j < index - 1; j++){
                books[j] = books[j + 1];
            }
            books[--index] = null;
            i--;
        }
    }
}

nrF is the number of a sentence's words.I dont understand why books[j] is equal to books[j+1]?

I assume you are not the author of this method.

A single = is not an equality check in Java, it is an assignment operator.

books[j] = books[j + 1];

means "books[j] has now the value of books[j+1]".

Doing this on every subsequent element of the array will effectively remove the specified element of said array.

Here's an 惊人的图 : you simply "compress" the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM