是否可以在 Dictionary 对象中包含一个数组 [英] Is it possible to have a array inside a Dictionary object

查看:33
本文介绍了是否可以在 Dictionary 对象中包含一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道组合到类似的数据结构是不合逻辑的,但很想知道我们是否可以在 Dictionary 对象中包含一个数组元素.类似的东西:

I knew that combining to similar data structures wouldn't be logical but curious to know if we can have a array element inside Dictionary object. some thing like:

Set d = CreateObject("Scripting.Dictionary")

d.Add "Name", "John"
d.Add "Age", 31
d.Add "Company", Array("microsoft", "apple")

推荐答案

您离自己解决问题只有一个 WScript.Echo:

You are one WScript.Echo away from solving the question for yourself:

>> Set d = CreateObject("Scripting.Dictionary")
>> d.Add "Company", Array("microsoft", "apple")
>> WScript.Echo Join(d("Company"))
>>
microsoft apple

cf 这个问题

更新(感谢@Ansgar):

.Item()(和For Each)传递的元素是副本;和数组赋值也复制(不像其他语言那样引用).因此,更改存储在字典中的数组元素意味着分配一个新数组:

The elements delivered by .Item() (and For Each) are copies; and Array assignment copies too (does not take a reference as in other languages). So changing an array element stored in a dictionary means assigning a new array:

>> Set d = CreateObject("Scripting.Dictionary")
>> d.Add "Company", Array("microsoft", "apple")
>> WScript.Echo Join(d("Company"))
>> d("Company") = Array(d("Company")(1), "samsung")
>> WScript.Echo Join(d("Company"))
>>
microsoft apple
apple samsung

有时使用(另一个)字典、System.Collections.Arraylist 或自定义对象(所有对象都是引用,因此赋值可以访问原始元素)更方便.

Sometimes it's more convenient to use a(nother) dictionary, a System.Collections.Arraylist, or a custom object (all objects are references, so an assignment gives access to the original element).

这篇关于是否可以在 Dictionary 对象中包含一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆