benkas Posted December 6, 2007 at 09:30 PM Report Share #152753 Posted December 6, 2007 at 09:30 PM Boas!! Alguem me sabe dizer como comparo cada posição de 1array de Strings ? Link to comment Share on other sites More sharing options...
magician Posted December 6, 2007 at 09:37 PM Report Share #152756 Posted December 6, 2007 at 09:37 PM Como assim :S ? explica lá isso melhor I haven’t lost my mind; it’s backed up on DVD somewhere! Link to comment Share on other sites More sharing options...
benkas Posted December 6, 2007 at 09:45 PM Author Report Share #152759 Posted December 6, 2007 at 09:45 PM por exemplo: {"carro", "mota", "aviao", "carro","bicicleta", "carro"} agora quero compara a 1ª posição, que é carro, com as restantes, "mota", "aviao", "carro","bicicleta", "carro", depois avança 1 posição no array, e vamos comparar mota com as restantes posições do array,"aviao", "carro","bicicleta", "carro" Link to comment Share on other sites More sharing options...
Knitter Posted December 6, 2007 at 11:07 PM Report Share #152790 Posted December 6, 2007 at 11:07 PM Algo deste género se percebi bem: public static void main(String[] args) { String[] teste = {"carro", "mota", "aviao", "carro", "bicicleta", "carro"}; int y; for (int z = 0; z < teste.length - 1; z++) { for (y = z + 1; y < teste.length; y++) { System.out.println("Comparar " + teste[z] + " com " + teste[y]); if (teste[z].compareTo(teste[y]) == 0) { System.out.println("Encontrado igual: " + teste[y]); } } } } www.sergiolopes.eu Link to comment Share on other sites More sharing options...
magician Posted December 7, 2007 at 01:02 AM Report Share #152814 Posted December 7, 2007 at 01:02 AM Algo deste género se percebi bem: public static void main(String[] args) { String[] teste = {"carro", "mota", "aviao", "carro", "bicicleta", "carro"}; int y; for (int z = 0; z < teste.length - 1; z++) { for (y = z + 1; y < teste.length; y++) { System.out.println("Comparar " + teste[z] + " com " + teste[y]); if (teste[z].compareTo(teste[y]) == 0) { System.out.println("Encontrado igual: " + teste[y]); } } } } Sim pelo que percebi deve ser isso também pode usar o equals ao invés do compareTo. I haven’t lost my mind; it’s backed up on DVD somewhere! Link to comment Share on other sites More sharing options...
Knitter Posted December 7, 2007 at 04:06 PM Report Share #152897 Posted December 7, 2007 at 04:06 PM Sim, seria possível usar o método equals em vez do compareTo, mas atenção que isso é possível porque a classe String redefine o método equals. www.sergiolopes.eu Link to comment Share on other sites More sharing options...
benkas Posted December 10, 2007 at 03:27 PM Author Report Share #153367 Posted December 10, 2007 at 03:27 PM obrigado pela dica. é isso mesmo 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