String counter in Arraylist

Asked

Viewed 492 times

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:

inserir a descrição da imagem aqui

  • But this is a question completely different from the other, I do not find it interesting to post in the same question

2 answers

0

Use a Map and the the method compute to assemble a word histogram. Using TreeMap, ordination already comes from grace:

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

class Frequencias {
    public static void main(String[] args) {
        List<String> dados = new ArrayList<>();
        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");

        Map<String, Integer> frequencias = new TreeMap<>();
        for (String c : dados) {
            frequencias.compute(c, (k, v) -> v == null ? 1 : v + 1);
        }

        frequencias.forEach((k, v) -> System.out.println(k + " aparece " + v + " vezes."));
    }
}

This is the output produced:

Dado 1 aparece 2 vezes.
Dado 2 aparece 2 vezes.
Dado 3 aparece 4 vezes.

See here working on ideone.

0

To count the number of occurrences of the same object/record in a list you can use the static method frequency() class Collections.

You need to just do:

int qtdDado1 = Collections.frequency(dados, "Dado 1");
int qtdDado2 = Collections.frequency(dados, "Dado 2");
int qtdDado3 = Collections.frequency(dados, "Dado 3");

Source: Mkyong

  • But in the future, the values will be entered from a txt, as I would a generic form of that code?

  • @Lucassilva as well?

  • All the data entered in Arraylist are temporary, just to do the structure, once finished, I will overwrite to an option that it receives the data from a txt, then it reads and inserts the data in Arraylist

Browser other questions tagged

You are not signed in. Login or sign up in order to post.