数组和哈希有什么区别?
Ruby 中的数组和哈希有什么区别?
What's the difference between arrays and hashes in Ruby?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Ruby 中的数组和哈希有什么区别?
What's the difference between arrays and hashes in Ruby?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
来自 Ruby-Doc:
数组是任何对象的有序、整数索引的集合。数组索引从 0 开始,如 C 或 Java 中一样。假定负索引相对于数组末尾,即索引 -1 表示数组的最后一个元素,-2 表示数组中倒数第二个元素,依此类推。请参阅此处了解更多信息。
哈希是键值对的集合。它类似于数组,不同之处在于索引是通过任何对象类型的任意键而不是整数索引完成的。哈希按照相应键插入的顺序枚举它们的值。
哈希有一个默认值,当访问哈希中不存在的键时会返回该默认值。默认情况下,该值为 nil。请参阅此处了解更多信息。
From Ruby-Doc:
Arrays are ordered, integer-indexed collections of any object. Array indexing starts at 0, as in C or Java. A negative index is assumed to be relative to the end of the array—that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on. Look here for more.
A Hash is a collection of key-value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.
Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil. Look here for more.
数组:
数组用于存储数据集合。数组中的每个对象都分配有一个唯一的键。我们可以使用这个唯一的键访问数组中的任何对象。数组中的位置从“0”开始。第一个元素位于“0”,第二个元素位于第一个位置,依此类推。
示例:
在 -irb 中尝试以下操作。
您已在数组中添加了 4 个元素。
将新元素添加到位置 5。
哈希值:
与数组一样,哈希也用于存储数据。哈希将一个对象指向另一个对象。考虑为字符串分配特定的“含义”。每次您引用该字符串时,它都会引用其“含义”。
示例:
现在尝试一下:
您会得到 => “Pulsar 220、Pulsar 200、Pulsar 180 和 Pulsar 150”
Arrays:
Arrays are used to store collections of data. Each object in an array has an unique key assigned to it. We can access any object in the array using this unique key. The positions in an array starts from " 0 ". The first element is located at " 0 ", the second at 1st position etc.
Example:
Try the following in - irb.
You have added 4 elements in the array.
Add a new element to position 5.
Hashes:
Like arrays, Hashes are also used to store data. Hashes points an object to another object. Consider of assigning a certain "meaning" to a string. Each time you refer that string, it refers its "meaning".
Example:
Try this now:
You get => "Pulsar 220, Pulsar 200, Pulsar 180 and Pulsar 150"
数组是事物的有序列表:a、b、c、d
哈希是键/值对的集合:john 拥有一辆标致,bob 拥有一辆雷诺,adam 拥有一辆福特。
An array is an ordered list of things: a, b, c, d
A hash is a collection of key/value pairs: john has a peugeot, bob has a renault, adam has a ford.
如今,这两个术语被“混杂”在一起。我认为事情是这样的:
“哈希”将有密钥 ->值对:
(上衣 -> T 恤,下装 -> 短裤,脚 -> 鞋子)
并且“数组”通常有一个索引:
([0]tshirt, [ 1]短裤,[2]鞋子)
但是,无论对错,你都会看到带有 key -> 的东西。值对也称为“数组”。
我认为差异主要取决于您想要何时以及如何使用它们。将数组称为哈希不会遇到太多麻烦,反之亦然,但您应该知道其中的区别。
The two terms get "hashed" together these days. I think this is how it goes:
A "hash" will have key -> value pairs:
(top -> tshirt, bottom -> shorts, feet -> shoes)
And an "array" will typically have an index:
([0]tshirt, [1]shorts, [2]shoes)
But, right or wrong, you'll see things with key -> value pairs called "arrays", too.
I think the difference depends mainly on when and how you want to use them. You won't get into much trouble calling an array a hash, or vice versa, but you should know the difference.