I'm implementing a priority queue using a vector, so my insert method works normally:
public boolean inserir(int n){
if(estaCheia()) {
return false;
}
if(estaVazia()) {
fila[nItens++] = n;
return true;
} else {
int i;
for(i = nItens-1; i>=0; i--) {
if(n > fila[i]){
fila[i+1] = fila[i];
} else{
break;
}
}
fila[i+1] = n;
nItens++;
return true;
}
}
The only problem is when I retrieve the first element of the queue, I get a free position in the vector, but when trying to insert an expection ArrayIndexOutOfBounds appears, I believe it is because the free space in the vector is the first. What should I do to rearrange the vector so that it can be inserted into it after removing the first element from the row?