什么是数组和哈希的区别? [英] What's the difference between arrays and hashes?

查看:551
本文介绍了什么是数组和哈希的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是Ruby的数组和散列的区别?

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

推荐答案

从Ruby的文件:

数组是有序的,任何物体的整数索引集合。数组索引从0开始,在C或Java。负索引被假设为相对于所述阵列,也就是说,索引-1指示数组的最后一个元件的端部,-2是阵列中的下一个到最后一个元素,依此类推。这里看更多。

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.

这篇关于什么是数组和哈希的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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