集合 vs 数组,区别 [英] Set vs Array , difference

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

问题描述

Ruby 中的 SetArray 有什么区别,除了集合保留唯一元素而数组保留重复元素之外?

What is the difference between Set and Array in Ruby except for the fact that sets keep unique elements while arrays can keep duplicate elements?

推荐答案

它们非常不同.

  • 数组是对象的有序列表.
  • 可以通过引用其在列表中的整数位置(从零开始)来访问数组值:a[3] 引用数组中的第 4 个对象.
  • 对可以是什么值没有限制——数组中允许有重复值.
  • 数组有一个对象字面表示法:[1, 'apple', String, 1, :banana](这会创建并初始化一个新数组).
  • 核心 ruby​​ 库中内置了数组.
  • An array is an ordered list of objects.
  • An array value can be accessed by referencing its integer position in the list (zero-indexed): a[3] references the 4th object in the array.
  • There is no restriction on what the values can be—duplicate values are allowed in arrays.
  • An array has an object literal notation: [1, 'apple', String, 1, :banana] (this creates and initializes a new Array).
  • Arrays are built in to the core ruby library.
  • 集合是唯一对象的无序池.
  • 由于它是无序的,因此没有可用于访问集合的特定元素的整数索引.
  • 唯一性限制意味着您不能在集合中拥有多个值的副本.
  • Set 不是核心的一部分,而是标准库的一部分,因此需要一个require 'set'.
  • 在 Ruby 2.4 之前,没有集合的对象字面量表示法,您必须通过 Set.new 创建它们.
    • 对于 Ruby >= 2.4.0,您可以使用 Set[](例如Set[1,2,3])
    • A set is an unordered pool of unique objects.
    • Since it's unordered, there is no integer index you can use to access specific elements of a set.
    • The uniqueness restriction means you can't have more than one copy of a value in the set.
    • Set is not part of the core, but part of the standard library, and thus needs a require 'set'.
    • Before Ruby 2.4, there was no object literal notation for sets, you had to create them via Set.new.
      • For Ruby >= 2.4.0 you can use Set[] (e.g. Set[1,2,3])

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

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