如何将字符串排序到映射中并打印结果
我有一个格式为 nm=Alan&hei=72&hair=brown
的字符串,
我想将此信息分开,添加对第一个值的转换并以
nm Name Alan
hei Height 72
hair Hair Color brown
I' 格式打印结果我们研究了使用 split 函数和哈希图的各种方法,但没有将它们拼凑在一起。
任何建议对我来说都非常有用。
I have a string in the format nm=Alan&hei=72&hair=brown
I would like to split this information up, add a conversion to the first value and print the results in the format
nm Name Alan
hei Height 72
hair Hair Color brown
I've looked at various methods using the split function and hashmaps but have had no luck piecing it all together.
Any advice would be very useful to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我真的不明白你的问题是什么......
I really do not understand what you problem was...
尝试这样的事情:
Try something like this:
HashMap 由许多键、值对组成。因此,当您使用 split,设计一个合适的正则表达式 (&)。获得字符串数组后,您可以使用其中一个元素作为键(考虑哪个元素将成为最佳键)。但是,您现在可能想知道“如何将其余元素放置为值?”。也许您可以创建一个新类来存储其余元素并使用此类的对象作为哈希图的值。
然后打印就变得容易了——只需搜索相应键的值即可。该值将是一个对象;在此对象上使用适当的方法来检索元素,您应该能够打印所有内容。
另外,请记住处理代码中的异常。例如检查空值等。
另一件事:你的 qn 提到了“排序”这个词。我不完全明白这在这种情况下意味着什么......
A HashMap consists of many key, value pairs. So when you use split, devise an appropriate regex (&). Once you have your string array, you can use one of the elements as the key (think about which element will make the best key). However, you may now be wondering- "how do I place the rest of elements as the values?". Perhaps you can create a new class which stores the rest of the elements and use objects of this class as values for the hashmap.
Then printing becomes easy- merely search for the value of the corresponding key. This value will be an object; use the appropriate method on this object to retrieve the elements and you should be able to print everything.
Also, remember to handle exceptions in your code. e.g. check for nulls, etc.
Another thing: your qn mentions the word "sort". I don't fully get what that means in this context...