問題描述
問題背景
我一直在開發一些代碼,首先關注的是讀取字符串和創建文件.其次,將字符串拆分為數組.然后獲取數組中每個單詞的索引,最后刪除重復項并將其打印到不同的文件中.我目前已經為此制作了代碼,這是一個鏈接 https://pastebin.com/gqWH0x0 (有一個菜單系統)但它相當長,所以我沒有在這個問題中實現它.
I have been developing some code that focuses on firstly, reading a string and creating a file. Secondly, spliting a string into an array. Then getting the indexes for each word in the array and finally, removing the duplicates and printing it to a different file. I currently have made the code for this here is a link https://pastebin.com/gqWH0x0 (there is a menu system as well) but it is rather long so I have refrained from implementing it in this question.
壓縮方法是通過 hashmaps 完成的,獲取數組的索引并將它們映射到相關的單詞.這是一個例子:
The compression method is done via hashmaps, getting indexes of the array and mapping them to the relevant word. Here is an example:
原文:《海見海見海見》
Original: "sea sea see sea see see"
輸出:見[2, 4, 5],sea[0, 1, 3],
Output: see[2, 4, 5],sea[0, 1, 3],
問題
下一階段是將輸出恢復到原始狀態.我目前對 java 比較陌生,所以我不知道所需的技術.代碼應該能夠獲取輸出文件(如上所示)并將其放回原始文件.
The next stage is getting the output back into the original state. I am currently relatively new to java so I am not aware of the techniques required. The code should be able to take the output file (shown above) and put it back into the original.
我目前的想法是您只需重寫此哈希圖(如下).我這樣想對嗎?我想我應該先檢查堆棧溢出!
My current thinking is that you would just rewrite this hashmap (below). Would I be correct in thinking this? I thought I should check with stack overflow first!
Map<String, Set<Integer>> seaMap = new HashMap<>(); //new hashmap
for (int seaInt = 0; seaInt < sealist.length; seaInt++) {
if (seaMap.keySet().contains(sealist[seaInt])) {
Set<Integer> index = seaMap.get(sealist[seaInt]);
index.add(seaInt);
} else {
Set<Integer> index = new HashSet<>();
index.add(seaInt);
seaMap.put(sealist[seaInt], index);
}
}
System.out.print("Compressed: ");
seaMap.forEach((seawords, seavalues) -> System.out.print(seawords + seavalues + ","));
System.out.println("
");
如果有人有任何好的想法/答案,請告訴我,我真的很渴望解決方案!
If anyone has any good ideas / answers then please let me know, I am really desperate for a solution!
鏈接到當前代碼:https://pastebin.com/gqWH0x0K
推薦答案
首先,您必須使用您的示例將帶有索引的單詞與壓縮行分開:
first you will have to separate the words with index(es) from your compressed line, using your example:
"see[2, 4, 5],sea[0, 1, 3],"
獲取以下字符串:
"see[2, 4, 5]" and "sea[0, 1, 3]"
對于每個您必須閱讀的索引,例如首先:
for each you must read the indexes, e.g. for first:
2, 4 and 5
現在只需在給定索引處的 ArrayList(或數組)中寫入單詞.
now just write the word in an ArrayList (or array) at the given index.
對于前兩個步驟,您可以使用正則表達式來查找每個單詞和索引列表.然后使用 String.split 和 Integer.parseInt 獲取所有索引.
For the first two steps you can use a regular expression to find each word and the index list. Then use String.split and Integer.parseInt to get all indexes.
Pattern pattern = Pattern.compile("(.*?)\[(.*?)\],");
String line = "see[2, 4, 5],sea[0, 1, 3],";
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
String word = matcher.group(1);
String[] indexes = matcher.group(2).split(", ");
for (String str : indexes) {
int index = Integer.parseInt(str);
現在只需檢查結果列表是否足夠大并將單詞設置在找到的索引處.
Now just check that the result List is big enough and set the word at the found indexes.
這篇關于如何獲取壓縮文件(通過索引)并重新創建原始文件?(爪哇)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!