1
I need that in the repetition it identifies the same strings, I know that I need to use the . equals(), however, is giving error, as if the variable x is checking more value that exist in Arraylist, but has as a condition that x is smaller than "data.size()" (which is the size of my vector).
public static void main(String args[]) {
ArrayList<String> dados= new ArrayList<String>();
//OBS. Primeiramente inserir os dados, futuramente
//ler arquivos txt
dados.add("Dado 2");
dados.add("Dado 1");
dados.add("Dado 3");
dados.add("Dado 3");
dados.add("Dado 3");
Collections.sort(dados);
//while(dados.contains(dados)){
// System.out.println("deu certo");
//}
//for (String x : dados){
// System.out.println(x);
// if (x.contains(x))
// }
int i;
int contador = 0;
int x = 0;
int tamanho = dados.size();
for (i = 0; i<tamanho; i++){
System.out.println(dados.get(i));
if (x<dados.size() && dados.get(i).equals(dados.get(++x))){
System.out.println("entrou no contador");
contador++;
}
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new main().setVisible(true);
}
});
}
Error generated from code:
[ERRO] run: Dado 1 Dado 2 Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 5 Dado 3 entrou no contador Dado 3 entrou no contador Dado 3 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at projetoic.main.main(main.java:118) C:\Users\lsilv\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 FALHA NA CONSTRUÇÃO (tempo total: 5 segundos)
By way of comment, it would be better if you put the code in the question instead of image to make it easier for those who help you ;)
– rLinhares
If you need to be
< tamanho
and not<= tamanho
.– Jéf Bueno
Puts the code in text form, in image form hinders testing,.
– user28595
I edited and added the project code, made the proper corrections sent here, but the error persists..
– Lucas Silva