Java:如何在Arraylist <integer>中找到最可重复的公用数字序列?

时间:2019-03-17 10:38:36

标签: java arraylist sequence

我需要在Arraylist<Integer>中找到最可重复的公用数字序列,然后将其显示在屏幕上。

例如:Arraylist将是992222223555,程序必须显示222222

我正在考虑将列表转换为String,并且可能会以某种方式找到常见字符的subString。

先谢谢您! 到目前为止,这是我的代码:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Zadacha8 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<String> dataBase = new ArrayList<>();
        Map<String, Integer> mapData = new HashMap<>();
        int count = 0;
        String curr;
        boolean flag;

        String listString = "";
        System.out.println("Please enter the array size: ");
        int broi = scanner.nextInt();
        for (int i = 0; i < broi; i++) {
            System.out.println("Please enter " + (i + 1) + "elemet: ");
            dataBase.add(i, String.valueOf(scanner.nextInt()));
        }
        // checking if one number is repeatable and putting it to a map (key - the repeatable number; value - how many times)
        for (int i = 1; i < broi; i++) {
            flag = false;
            if (dataBase.get(i).equals(dataBase.get(i - 1))) {
                count++;
                flag = true;
                continue;
            }
            if (flag = true && count != 0) {
                mapData.put(dataBase.get(i), count);
                count = 0;
            }
        }
        //System.out of the map just for check
        for (String name : mapData.keySet()) {
            String key = name.toString();
            String value = mapData.get(name).toString();
            System.out.println(key + " " + value);
        }
        // Next step - sorting the map .value

        //system.out.print value * key

    }
}

0 个答案:

没有答案