我如何将索引和单词放在一起以创建1个最终的字符串/数组?(How do I put indexes and words together to make 1 final string/array? (

编程入门 行业动态 更新时间:2024-10-09 20:28:28
我如何将索引和单词放在一起以创建1个最终的字符串/数组?(How do I put indexes and words together to make 1 final string/array? (Java))

问题的背景

我一直在开发能够读取字符串的代码,将其压缩并以压缩形式发送到新文件。 (例如“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 see

I 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

更多推荐

本文发布于:2023-08-02 09:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1372767.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   如何将   单词   放在一起

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!