Jump to content

Remover elemento do array


Recommended Posts

Boa tarde pessoal, eu ate me sinto mal em ter esta duvida mas estou com dificuldades em acertar com o algoritmo de remover de um array.

por exemplo se eu tiver o array = [1,2,3] e quiser ficar com array = [1,3] ; nao estou mesmo a conseguir ja pensei em fazer array[*i] = array[i+1] mas nao resulta pois fica do tipo array[1,3,3] alguem me pode ajudar?

Link to comment
Share on other sites

Contas o número de casas que vais remover, crias um novo array com a.length - num_de_casas_a_remover e copias os números que não são para remover para lá.

private int[] a;
public void remove(int numero){
for(int i = 0; i != a.lenght; i++){
         if(a[i] == numero){

E se tiveres que remover dois números iguais do array?

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.