问题的背景
我一直在开发能够读取字符串的代码,将其压缩并以压缩形式发送到新文件。 (例如“hello,hello” - >“hello [0,1]),目前这个工作很好,下面是任何想要使用它的人的链接: https : //pastebin.com/v6YF34mU 。正在能够使用索引和单词重新创建文件。
题
我目前有这样的代码:
public static void main(String[] args) { // TODO code application logic here Pattern seaPattern = Pattern.compile("(.*?)\\[(.*?)\\],"); String compressedSea = "see[2, 4, 5],sea[0, 1, 3],"; Matcher seaMatcher = seaPattern.matcher(compressedSea); while (seaMatcher.find()) { String seaWords = seaMatcher.group(1); String[] seaIndexes = seaMatcher.group(2).split(", "); for (String str : seaIndexes) { int seaIndex = Integer.parseInt(str); System.out.print(seaIndex); }简而言之。 我读取字符串并将其分成不同的数组。 其中一个数组包含索引,另一个包含单词。 下一个阶段是将它们放在一起并创建一个能够被压缩的字符串。 我对Java相对比较陌生,所以我不完全确定如何去做这件事。
如果任何人有任何想法如何做到这一点,将不胜感激!
Background to question
I have been working on developing code that is able to read a string, compress it an send it to a new file in a compressed form. (e.g. "hello, hello" -> "hello[0, 1]) and this works great at the moment. Here is the link for anyone who wants to use it: https://pastebin.com/v6YF34mU . The next stage is being able to re-create the file using the indexes and the words.
Question
I currently have got this code:
public static void main(String[] args) { // TODO code application logic here Pattern seaPattern = Pattern.compile("(.*?)\\[(.*?)\\],"); String compressedSea = "see[2, 4, 5],sea[0, 1, 3],"; Matcher seaMatcher = seaPattern.matcher(compressedSea); while (seaMatcher.find()) { String seaWords = seaMatcher.group(1); String[] seaIndexes = seaMatcher.group(2).split(", "); for (String str : seaIndexes) { int seaIndex = Integer.parseInt(str); System.out.print(seaIndex); }In short. I reads the string and splits it up into to different arrays. One of the arrays contains the indexes and the other contains the words. The next stage is putting these together and creating 1 string that is able to be compressed. I am relatively new to Java so I am not completely sure how I would go about doing this.
If anyone has got any ideas on how to do this it would be much appreciated!
最满意答案
我建议你创建一个结合你的单词和索引的类。
我在下面发布了一条建议:
OP澄清了他想要输出的内容后更新了答案
public static void main(String[] args) throws FileNotFoundException { Map<Integer, String> wordMap = new HashMap<Integer, String>(); Pattern seaPattern = Pattern.compile("(.*?)\\[(.*?)\\],"); String compressedSea = "see[2, 4, 5],sea[0, 1, 3],"; Matcher seaMatcher = seaPattern.matcher(compressedSea); while (seaMatcher.find()) { String word = seaMatcher.group(1); String[] seaIndexes= seaMatcher.group(2).split(", "); for(String s : seaIndexes){ wordMap.put(Integer.valueOf(s), word); } } //HashMap will printout ordered by the key value. //This is because the key is an Integer //The hashed key value is therefore the key value itself. System.out.println(wordMap); }产量
{0=sea, 1=sea, 2=see, 3=sea, 4=see, 5=see}奖金
如果你想通过HashMap迭代,你可以执行以下操作:
Iterator it = wordMap.entrySet().iterator(); while(it.hasNext()){ Map.Entry pair = (Map.Entry)it.next(); System.out.println(pair.getValue()); }产量
sea sea see sea see seeI would suggest you to create a class for combining your word and indexes.
I have posted a suggestion below:
Updated answer after OP clarified what he wanted to output
public static void main(String[] args) throws FileNotFoundException { Map<Integer, String> wordMap = new HashMap<Integer, String>(); Pattern seaPattern = Pattern.compile("(.*?)\\[(.*?)\\],"); String compressedSea = "see[2, 4, 5],sea[0, 1, 3],"; Matcher seaMatcher = seaPattern.matcher(compressedSea); while (seaMatcher.find()) { String word = seaMatcher.group(1); String[] seaIndexes= seaMatcher.group(2).split(", "); for(String s : seaIndexes){ wordMap.put(Integer.valueOf(s), word); } } //HashMap will printout ordered by the key value. //This is because the key is an Integer //The hashed key value is therefore the key value itself. System.out.println(wordMap); }Output
{0=sea, 1=sea, 2=see, 3=sea, 4=see, 5=see}Bonus
If you want to iterate though the HashMap, you can do the following:
Iterator it = wordMap.entrySet().iterator(); while(it.hasNext()){ Map.Entry pair = (Map.Entry)it.next(); System.out.println(pair.getValue()); }Output
sea sea see sea see see更多推荐
发布评论