简体   繁体   中英

Remove objects from a list through an array of integers?

Hi community I have a question, I happen to have an array of objects loaded on startup, through that generate array another array of integers that contains your code, it appears that array of integers'm removing their values, what I want is to compare the list of integer array currently have with the array of objects, and remove all code object that whole array mentioned is found.

My code java:

private List<ValidColumnKey> columnCustomer; 
private int[] selectedCustomer;

public void init(){
    this.setColumnCustomer(new ArrayList<ValidColumnKey>());        
    this.getColumnCustomer().add(new ValidColumnKey(1, "Codigo", "code"));  
    this.getColumnCustomer().add(new ValidColumnKey(2, "Nombre", "name"));  
    this.getColumnCustomer().add(new ValidColumnKey(3, "Nombre Comercial", "comercialName"));  
    this.getColumnCustomer().add(new ValidColumnKey(4, "Estado", "isActive")); 

    this.setSelectedCustomer(new int [this.getColumnCustomer().size()]);
    int i = 0;
    for(ValidColumnKey column : this.getColumnCustomer()){
        this.getSelectedCustomer()[i] = column.getCodigo();
        i++;
    }
}

I mean I would have my array of integers with codes removed, like this:

selectedCustomer = [1, 2, 3];

What I wanted was to remove from the list of objects that do not have codes in the array of integers, but it is not my code:

List<ValidColumnKey> auxRemoColumnKeys = new ArrayList<ValidColumnKey>();
for(ValidColumnKey column : this.getColumnCustomer()){
    for(Integer codigo : this.getSelectedCustomer()){
        if (column.getCodigo() != codigo) {
            auxRemoColumnKeys.add(column);
            break;
        }
    }           
}
this.getColumnCustomer().remove(auxRemoColumnKeys);

I could guide the solution.

this.getColumnCustomer().remove(auxRemoColumnKeys);

This statement assumes you have a valid equals method for your class ValidColumnKey , which I suspect is probably not the case.

What you want to do is iterate with a Iterator . Some sample code could be like

Set<Integer> toRemoveCodes = new HashSet<Integer>(Arrays.asList(1, 2, 3));
for (Iterator<ValidColumnKey> it = this.getColumnCustomer().iterator(); it.hasNext(); ) {
   ValidColumnKey curColumnKey = it.next();
   Integer code = curColumnKey.codigo();
   if (toRemoveCodes.contains(code)) {
       it.remove();
   } 

}

There are multiple reasons your current attempt is failing. The first is that this line:

if (column.getCodigo() != codigo) {

Is testing for object equivalence between Integer s, not value equavalence between int s. If you want to compare Integer s, you have to use the equals method:

if (!column.getCodigo().equals(codigo)) {

However, if getCodigo returns an int and getSelectedCustomer returns an int[] then this line should be changed instead:

for(int codigo : this.getSelectedCustomer()){

Because you didn't need to use Integer in the first place.

Secondly, this line attempts to remove auxRemoColumnKeys itself so you probably mean removeAll :

this.getColumnCustomer().remove(auxRemoColumnKeys);

Lastly, your logic is generally flawed. It basically says "for each element in getColumnCustomer, if getCodigo is not equal to all of getSelectedCustomer remove it" . I don't think that's what you've intended.

This is a modified loop that uses the same "add to a list and remove the list items" procedure but the logic will work:

List<ValidColumnKey> auxRemoColumnKeys = new ArrayList<ValidColumnKey>();
int[] selected = this.getSelectedCustomer();

for (ValidColumnKey column : this.getColumnCustomer()) {
    int i = 0;
    for ( ; i < selected.length; i++) {

        /* note: if getCodigo returns an Integer change this check to
         * "if (column.getCodigo().equals(selected[i])) {"
         */
        if (column.getCodigo() == selected[i]) {
            break;
        }
    }

    /* this says "if the search loop did not break early" */
    if (i == selected.length) {
        auxRemoColumnKeys.add(column);
    }
}
this.getColumnCustomer().removeAll(auxRemoColumnKeys);

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