0
I need the program to return the amount of times each String
repeated itself within the ArrayList
, but I could not think of a way in which my counter is reused for each item, because as it shows there at the exit, says that data 3 repeated 4, 5 and 6 times. It would be better to do a public service and just call it in main
? How can I solve?
public static void main(String args[]) {
ArrayList<String> dados= new ArrayList<String>();
dados.add("Dado 2");
dados.add("Dado 1");
dados.add("Dado 3");
dados.add("Dado 3");
dados.add("Dado 2");
dados.add("Dado 1");
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 = 1;
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++))){
contador++;
}
System.out.print("repetiu:"); System.out.println(contador+1);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new main().setVisible(true);
}
});
}
Exit code:
But this is a question completely different from the other, I do not find it interesting to post in the same question
– Lucas Silva