How can I delete an object from a vector, and then rearrange the vector? [duplicate]

0

I need to delete an object from a vector, regardless of the position, and if the object leaves an empty space, the vector must be re-accommodated until the last empty space is left, this is what I have:

public void remove(short pos){
    boolean flag = false;
    for (short posRemove = pos; posRemove < listVuelos.length -1 && !flag; posRemove ++){
        if(listVuelos[posRemove +1] != null){
        listVuelos[posRemove] = listVuelos[posRemove + 1];
        }
        else {
            flag = true;
            listVuelos[posRemove] = null;
        }
    }
}
    
asked by Yeferson Gallo 22.10.2016 в 05:59
source

1 answer

0

Keep in mind that the data type short the default values of this is 0 and not null to make the comparison. for example when you make a short[] arr = new short[10]; all the initial values of your array will be 0 . Setting the value of the position exchanged to 0 would be an option

Considering @sstan's comment, and what the documentation says datatypes the only primitive data that can be compared with null would be String and / or Objects, the rest when creating a value different from null if your fix data type is String keep using null in comparison if

public void remove(short pos){
boolean flag = false;
 for (short posRemove = pos; posRemove < listVuelos.length -1 && !flag; posRemove ++){
    /* Comparar con 0 , y no con null */ 
    if(listVuelos[posRemove +1]!=0){ /*Si es string sería != null */
        listVuelos[posRemove] = listVuelos[posRemove + 1];
        listVuelos[posRemove+1]=0; /* Setear a 0*/
    }
    else {
        flag = true;
        listVuelos[posRemove] = 0;
    }
  }
}
    
answered by 22.10.2016 / 06:49
source