gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:23:10 25 4
gpt4 key购买 nike

我有一些数组列表字符串,里面有关键字,就像这样!

Windows 是数组列表字符串,关键字为粗体 窗口结构:前9个字+关键字+后9个字

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

如何组合该数组列表来接收这样的信息:

alt text

谢谢

最佳答案

如果你不太担心性能,一个简单的 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 joined 的“尾部”之间的匹配和 textAsList 的“头” .

关于java - java中如何组合数组列表字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2901635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com