malucodojava Posted February 17, 2012 at 08:02 PM Report Share #440115 Posted February 17, 2012 at 08:02 PM 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 More sharing options...
lesiano16 Posted February 17, 2012 at 08:17 PM Report Share #440119 Posted February 17, 2012 at 08:17 PM >Adicionas elemento a elemento do array para um arrayList exceptuando o que queres eliminar. > Convertes a lista para array utilizando o ".toArray()"; > Igualas o array ao resultado da conversão. Link to comment Share on other sites More sharing options...
malucodojava Posted February 17, 2012 at 08:20 PM Author Report Share #440122 Posted February 17, 2012 at 08:20 PM eu nao queria fazer com arraylists , eu sei que facilita o trabalho eu uso sempre arraylists mas desta vez nao posso Link to comment Share on other sites More sharing options...
lesiano16 Posted February 17, 2012 at 08:21 PM Report Share #440123 Posted February 17, 2012 at 08:21 PM >Adicionas elemento a elemento do array para outro array exceptuando o que queres eliminar. > Igualas o array antigo ao novo array. Podes até usar o System.arraycopy Link to comment Share on other sites More sharing options...
Baderous Posted February 17, 2012 at 08:22 PM Report Share #440124 Posted February 17, 2012 at 08:22 PM Tens de fazer shift de todos os elementos subsequentes ao que queres eliminar, 1 casa para a esquerda. Link to comment Share on other sites More sharing options...
malucodojava Posted February 17, 2012 at 08:31 PM Author Report Share #440126 Posted February 17, 2012 at 08:31 PM isso seria: private int[] a; public void remove(int numero){ for(int i = 0; i != a.lenght; i++){ if(a[i] == numero){ a[i]=a[i+1]; } Link to comment Share on other sites More sharing options...
lesiano16 Posted February 17, 2012 at 08:37 PM Report Share #440128 Posted February 17, 2012 at 08:37 PM Tu queres fazer a partir do momento que a == numero e não apenas para este. Como o que tu fizeste se tiveres [1,2,3,4] e removeres 3 daria [1,2,4,4] Link to comment Share on other sites More sharing options...
malucodojava Posted February 17, 2012 at 08:45 PM Author Report Share #440132 Posted February 17, 2012 at 08:45 PM certo, como faço entao quando chego ao fim, ja nao há mais nada na esquerda o que é que ele mete? Link to comment Share on other sites More sharing options...
KTachyon Posted February 17, 2012 at 09:02 PM Report Share #440135 Posted February 17, 2012 at 09:02 PM 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){ a[i]=a[i+1]; } 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now