请将代码完整地输出给我
时间: 2023-07-31 09:10:01 浏览: 55
以下是完整代码:
```java
import java.io.*;
import java.util.*;
public class IdiomCounter {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("D:\\yy\\java\\成语接龙\\成语大全\\idiom.txt"));
List<String> idiomList = new ArrayList<>();
String line;
while ((line = br.readLine()) != null) {
String[] idiomArray = line.split(" ");
idiomList.addAll(Arrays.asList(idiomArray));
}
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\yy\\java\\成语接龙\\成语大全\\全量成语处理后.txt"));
Map<Character, List<String>> idiomMap = new HashMap<>();
for (String idiom : idiomList) {
if (!idiom.isEmpty()) {
char lastChar = idiom.charAt(idiom.length() - 1);
List<String> idiomSet = idiomMap.getOrDefault(lastChar, new ArrayList<>());
idiomSet.addAll(Collections.singleton(idiom));
idiomMap.put(lastChar, idiomSet);
}
}
for (String idiom : idiomList) {
if (!idiom.isEmpty()) {
char lastChar = idiom.charAt(idiom.length() - 1);
int count = idiomMap.containsKey(lastChar) ? idiomMap.get(lastChar).size() : 0;
bw.write(idiom + "可接:" + count);
bw.newLine();
}
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这个程序从 `idiom.txt` 文件中读取成语列表,然后统计每个成语的可接成语数量,并将结果写入到 `全量成语处理后.txt` 文件中。在统计可接成语数量时,使用了 `HashMap` 来存储每个成语的可接成语列表,避免了重复计数的问题。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)