数组和哈希有什么区别?

发布于 2024-11-08 19:26:13 字数 25 浏览 0 评论 0原文

Ruby 中的数组和哈希有什么区别?

What's the difference between arrays and hashes in Ruby?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

饮惑 2024-11-15 19:26:13

来自 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.

ペ泪落弦音 2024-11-15 19:26:13

数组:
数组用于存储数据集合。数组中的每个对象都分配有一个唯一的键。我们可以使用这个唯一的键访问数组中的任何对象。数组中的位置从“0”开始。第一个元素位于“0”,第二个元素位于第一个位置,依此类推。

示例:
在 -irb 中尝试以下操作。

bikes = Array.new
bikes = %w[Bajaj-Pulsar, Honda-Unicorn, TVS-Apache, Yamaha, Suzuki]

您已在数组中添加了 4 个元素。

puts bikes[3]
Yamaha,

将新元素添加到位置 5。

bikes[5] = "Hardly Davidson"

哈希值:
与数组一样,哈希也用于存储数据。哈希将一个对象指向另一个对象。考虑为字符串分配特定的“含义”。每次您引用该字符串时,它都会引用其“含义”。

示例:

bikes = Hash.new
bikes = {
'Bajaj' => 'Pulsar 220, Pulsar 200, Pulsar 180 and Pulsar 150',
'Honda' => 'Unicorn, Shine and Splendor',
'TVS' => 'Apache, Star City, and Victor'
}

现在尝试一下:

bikes['Bajaj']

您会得到 => “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.

bikes = Array.new
bikes = %w[Bajaj-Pulsar, Honda-Unicorn, TVS-Apache, Yamaha, Suzuki]

You have added 4 elements in the array.

puts bikes[3]
Yamaha,

Add a new element to position 5.

bikes[5] = "Hardly Davidson"

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:

bikes = Hash.new
bikes = {
'Bajaj' => 'Pulsar 220, Pulsar 200, Pulsar 180 and Pulsar 150',
'Honda' => 'Unicorn, Shine and Splendor',
'TVS' => 'Apache, Star City, and Victor'
}

Try this now:

bikes['Bajaj']

You get => "Pulsar 220, Pulsar 200, Pulsar 180 and Pulsar 150"

老娘不死你永远是小三 2024-11-15 19:26:13

数组是事物的有序列表: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.

禾厶谷欠 2024-11-15 19:26:13

如今,这两个术语被“混杂”在一起。我认为事情是这样的:

“哈希”将有密钥 ->值对:

(上衣 -> 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文