如何在java中组合数组列表字符串?

时间:2010-05-25 02:34:50

标签: java

我有一些带有关键字的arraylist字符串就像那样!

windows是arraylist字符串,关键字是粗体  窗口结构:+关键字前9个字+

后9个字

您可以看到一些窗口重叠 alt text

如何将那个arraylist结合起来接收:

alt text

谢谢

2 个答案:

答案 0 :(得分:4)

如果您对性能不太担心,可以很容易地编写一个简单的subList/equals匹配:

    String[] texts = {
        "sunset lake michigan michigan alaska water florida "
        + "peninsula third largest water seventh largest water "
        + "percentage edit list largest country",

        "michigan alaska water florida peninsula third largest water "
        + "seventh largest water percentage edit list largest country "
        + "subdivision list political",

        "third largest water seventh largest water percentage edit list "
        + "largest country subdivision list political geographic "
        + "subdivisions total edit references"
    };
    List<String> joined = new ArrayList<String>();
    for (String text : texts) {
        List<String> textAsList = Arrays.asList(text.split(" "));
        final int N = joined.size();
        final int M = textAsList.size();
        for (int k = Math.min(N, M); k >= 0; k--) {
            if (joined.subList(N - k, N).equals(textAsList.subList(0, k))) {
                joined.addAll(textAsList.subList(k, M));
                break;
            }
        }
    }
    System.out.println(joined);

打印:

[sunset, lake, michigan, michigan, alaska, water, florida,
peninsula, third, largest, water, seventh, largest, water,
percentage, edit, list, largest, country, subdivision, list,
political, geographic, subdivisions, total, edit, references]

该算法的工作原理如下:要构建List<String> joined,给定List<String> textAsList,我们会发现subList的“尾部”与“{1}}之间的最长joined匹配“textAsList。”

答案 1 :(得分:0)

使用How to Use Editor Panes and Text Panes查看examples和这些DefaultHighlighter

附录:啊,我以为你只是需要这个观点。对于模型,请考虑Knuth–Morris–Pratt algorithm中讨论的answer