修改字典
我有一个 Dictionary
一些示例值是
Key1 Value="1","2","3","4","5"
Key2 Value="7","8"
Key3 Value=null
我希望数组长度是所有值的最大值,在我的例子中,Key1 是 5 所以我可以得到这样的结果:
Key1 Value="1","2","3","4","5"
Key2 Value="7","8","","",""
Key3 Value="","","","",""
所以所有的键都有相同的数组长度= 5,并且之前不存在的值是空值“”。 这怎么能做到呢?
I have a Dictionary<string,string[]>
Some example values are
Key1 Value="1","2","3","4","5"
Key2 Value="7","8"
Key3 Value=null
I want to the array length to be the max of all values which in my case is 5 for Key1
so I can get a result as:
Key1 Value="1","2","3","4","5"
Key2 Value="7","8","","",""
Key3 Value="","","","",""
So all Keys have same array length = 5 and the values which didnt exist before are empty values "".
How can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个
Try this
我会使用类似的方法将字典封装到我自己的类中,除非每当添加值时,如果必须扩展该数组以包含该值,则需要扩展字典中所有其他数组值的大小。
如果您追求效率,每次发生这种情况时我都会将数组加倍,以避免代码效率低下。您可以跟踪所有数组的虚拟“最大大小”,即使您通过在类 int 变量中跟踪它来有效地将它们加倍。
I'd encapsulate a Dictionary into my own class with similar methods, except whenever a value is added, if you must extend that array to contain the value, you extend the size of all other array values in the dictionary.
If you're going for efficiency, I'd double the arrays each time this happens to avoid inefficient code. You can keep track of the virtual 'max size' of all the arrays even if you're effectively doubling them by keeping track of it in a class int variable.