Ruby 数组(Array)

news/2025/3/3 7:17:33/

一、参数= 符号赋值数组对象 时,是指向同一个数组对象

eg:

ruby">irb(main):019:0> a = []   //定义一个数组
=> []
irb(main):020:0> b = a   //将变量a赋值给b,a和b指向同个内存
=> []
irb(main):021:0> a.object_id  //a的对象id
=> 25204596
irb(main):022:0> b.object_id  //b的对象id
=> 25204596
irb(main):023:0> a << 'foo'  //a写入值foo
=> ["foo"]
irb(main):024:0> b  // 打印b的值发现和a一样
=> ["foo"]

二、定义数组的方法

1.[] 定义数组

ruby">irb(main):025:0> a = ['1','2']
=> ["1", "2"]

2.Array.new():括号中可以输入定义数组的长度,和数组的相同值,修改其中一个,其他值都改变,数组的参数都指向同一个对象

ruby">irb(main):026:0> Array.new(3)  //定义一个长度为3个的无数据的数组
=> [nil, nil, nil]
irb(main):027:0> Array.new(3,"asdf") //定义有3个asdf 字符的数组
=> ["asdf", "asdf", "asdf"]
irb(main):028:0> b =Array.new(3,"asdf")//定义有3个asdf 字符的数组并赋值给b
=> ["asdf", "asdf", "asdf"]   
irb(main):029:0> b[0]  //b数组的第一参数的值
=> "asdf"
irb(main):030:0> b[1]  //b数组的第二参数的值
=> "asdf"
irb(main):031:0> b[0].object_id //b数组的第一参数对象的值
=> 25986720 
irb(main):032:0> b[1].object_id //b数组的第二参数对象的值
=> 25986720
irb(main):033:0> b[0][2]='m'  //b数组的第二数据asdf 第三个字符d 改为m
=> "m"
irb(main):034:0> b[0]
=> "asmf"
irb(main):035:0> b[1]
=> "asmf"
irb(main):036:0> b[2]
=> "asmf"

3.Array.new(){}:创建多个数组,值不一样,修改后不会都修改,可以修改指定值

ruby">irb(main):038:0> c = Array.new(2){'abcd'} //定义一个数组大小为2个,值都为abcd 的不同对象
=> ["abcd", "abcd"]
irb(main):039:0> c[0]   //查看数组第一个的值
=> "abcd"
irb(main):040:0> c[1]  //查看数组第二个的值
=> "abcd"
irb(main):041:0> c[0].object_id  //查看数组第一个值的对象id
=> 25344288 
irb(main):042:0> c[1].object_id //查看数组第二个值的对象id
=> 25344276
irb(main):043:0> c[1][2]='k'  //查看数组第二个值的字符串的第三个字符设置为k
=> "k"
irb(main):044:0> c[0]  //查看数组第一个的值
=> "abcd"
irb(main):045:0> c[1]  //查看数组第二个的值,可以看到只有第二个值改变了
=> "abkd"

3. %w(): 简单快速的定义数组,不需要输入单引号和逗号

ruby">irb(main):046:0> arr = %w(aa bb cc dd)
=> ["aa", "bb", "cc", "dd"]

三、数组的方法

1.[]:查看第几个数据

ruby">irb(main):048:0> a = %w(foo bar wut wat) //定义数组
=> ["foo", "bar", "wut", "wat"]
irb(main):049:0> a[0] //获取数组的第一个数据
=> "foo"

2.[..]:查看区间数据

ruby">irb(main):050:0> a[-1] //获取最后一个
=> "wat"
irb(main):051:0> a[1..2]
=> ["bar", "wut"]

3.fetch():安全的取数据,可以定义取不到数据的情况

ruby">irb(main):052:0> a.fetch(2)
=> "wut"
irb(main):053:0> a.fetch(4)
IndexError: index 4 outside of array bounds: -4...4from (irb):53:in `fetch'from (irb):53from D:/RailsInstaller/Ruby2.2.0/bin/irb:11:in `<main>'
irb(main):054:0> a.fetch(4,'asdf')
=> "asdf"

4.length

ruby">irb(main):055:0> a.length
=> 4

5.include?():数组是否包含哪些字符

ruby">irb(main):057:0> a.include?('foo')
=> true
irb(main):058:0> a.include?('aa')
=> false

6.empty?(): 是否为空数组

ruby">irb(main):059:0> a.empty?()
=> false

7.push():往数组后添加数据,也可以添加相同值

ruby">irb(main):060:0> a.push('haha')
=> ["foo", "bar", "wut", "wat", "haha"]

8.[]=:数组指定位置添加数据,如果中间没有数据,会创建空数据

ruby">irb(main):061:0> a[10]='aab'
=> "aab"
irb(main):062:0> a
=> ["foo", "bar", "wut", "wat", "haha", nil, nil, nil, nil, nil, "aab"]

9.delete_at():删除指定位置的值

ruby">irb(main):062:0> a
=> ["foo", "bar", "wut", "wat", "haha", nil, nil, nil, nil, nil, "aab"]
irb(main):063:0> a.delete_at(0)
=> "foo"
irb(main):064:0> a
=> ["bar", "wut", "wat", "haha", nil, nil, nil, nil, nil, "aab"]

10.delete():删除指定内容的值

ruby">irb(main):064:0> a
=> ["bar", "wut", "wat", "haha", nil, nil, nil, nil, nil, "aab"]
irb(main):065:0> a.delete('haha')
=> "haha"
irb(main):066:0> a
=> ["bar", "wut", "wat", nil, nil, nil, nil, nil, "aab"]

11.uniq!: 相同值删除,保存其中一个,不加感叹号

ruby">irb(main):067:0> a.push('bar')
=> ["bar", "wut", "wat", nil, nil, nil, nil, nil, "aab", "bar"]
irb(main):068:0> a.uniq()
=> ["bar", "wut", "wat", nil, "aab"]

12.shuffe: 将数组数据随机排序

ruby">irb(main):072:0> a.shuffle
=> ["bar", "wut", "aab", "wat", nil]
irb(main):073:0> a
=> ["bar", "wut", "wat", nil, "aab"]

13.falttern:二维数组转为1维数组

ruby">irb(main):001:0> a=[[1,2,34],['a','b','c']]  //创建二维数组
=> [[1, 2, 34], ["a", "b", "c"]]
irb(main):002:0> a.flatten  //将二维数组转为一维数组
=> [1, 2, 34, "a", "b", "c"]

四、遍历数组

1.each{}:遍历数组

ruby">irb(main):004:0> arr=['a','b','c','d'] //创建arr数组
=> ["a", "b", "c", "d"]
irb(main):006:0> arr.each{|e| p e} //遍历打印数组,eg |e|为变量e  p为打印的意思 e为变量
"a"
"b"
"c"
"d"
=> ["a", "b", "c", "d"]

2.reverse_each{}: 倒序遍历数组

ruby">irb(main):004:0> arr=['a','b','c','d']
=> ["a", "b", "c", "d"]
irb(main):007:0> arr.reverse_each{|e| p e} //倒序打印arr数组
"d"
"c"
"b"
"a"
=> ["a", "b", "c", "d"]

3.each_with_index: 下标遍历数组

ruby">irb(main):001:0> arr=['a','b','c','d']
=> ["a", "b", "c", "d"]
irb(main):002:0> arr.each_with_index{|e,i| p [e,i]}   //定义e和i变量,e为数组数据,i为数组的下标
["a", 0]
["b", 1]
["c", 2]
["d", 3]
=> ["a", "b", "c", "d"]

4.sort:数组数据从小到大排序

ruby">irb(main):003:0> arr=[-1,-2,-4,0,99,-2,8]
=> [-1, -2, -4, 0, 99, -2, 8]
irb(main):004:0> arr.sort
=> [-4, -2, -2, -1, 0, 8, 99]

5.select{}:遍历判断数组的值

ruby">irb(main):003:0> arr=[-1,-2,-4,0,99,-2,8]
=> [-1, -2, -4, 0, 99, -2, 8]
irb(main):005:0> arr.select{|e| e>0}  //数组元素大于0的数据
=> [99, 8]

6.compact: 删除数组里面空元素

ruby">irb(main):006:0> arr<< nil
=> [-1, -2, -4, 0, 99, -2, 8, nil]
irb(main):007:0> arr.compact  //删除nil 空白元素,ps要想对arr生效,可以用危险!eg:arr.compact!
=> [-1, -2, -4, 0, 99, -2, 8]
irb(main):008:0> arr
=> [-1, -2, -4, 0, 99, -2, 8, nil]
irb(main):009:0> arr.compact!
=> [-1, -2, -4, 0, 99, -2, 8]
irb(main):010:0> arr
=> [-1, -2, -4, 0, 99, -2, 8]

7.any?{}: 是否含有{条件}。eg:是否含有小于 零的数

ruby">irb(main):010:0> arr
=> [-1, -2, -4, 0, 99, -2, 8]
irb(main):011:0> arr.any?{|e| e<0}  //数组是否含有小于0的元素
=> true

更多array操作:https://ruby-doc.org//core-2.2.0/Array.html

课后练习

输入:input=[1,2,3]  输出:output = [2,4,6]

要求:

不能改变原input

output 是一个新的array object

尽量简洁

ruby">output= []input.each{|e| output << e*2}


http://www.ppmy.cn/news/1576231.html

相关文章

Grok 3 vs. DeepSeek vs. ChatGPT:2025终极AI对决

2025 年,AI 领域的竞争愈发激烈,三个重量级选手争夺霸主地位:Grok 3(由 xAI 开发)、DeepSeek(国内 AI 初创公司)和 ChatGPT(OpenAI 产品)。每个模型都有自己独特的优势,无论是在深度思考、速度、编程辅助、创意输出,还是在成本控制方面,都展现出强大的实力。但究竟…

PyQT(PySide)的上下文菜单策略设置setContextMenuPolicy()

在 Qt 中&#xff0c;QWidget 类提供了几种不同的上下文菜单策略&#xff0c;这些策略通过 Qt::ContextMenuPolicy 枚举类型来定义&#xff0c;用于控制控件&#xff08;如按钮、文本框等&#xff09;在用户右键点击时如何显示上下文菜单。 以下是 Qt::ContextMenuPolicy 枚举中…

PyCharm怎么集成DeepSeek

PyCharm怎么集成DeepSeek 在PyCharm中集成DeepSeek等大语言模型(LLM)可以借助一些插件或通过代码调用API的方式实现,以下为你详细介绍两种方法: 方法一:使用JetBrains AI插件(若支持DeepSeek) JetBrains推出了AI插件来集成大语言模型,不过截至2024年7月,官方插件主要…

LLMs之DeepSeek:DeepSeek-V3/R1推理系统的架构设计和性能统计的简介、细节分析之详细攻略

LLMs之DeepSeek&#xff1a;DeepSeek-V3/R1推理系统的架构设计和性能统计的简介、细节分析之详细攻略 目录 DeepSeek-V3/R1推理系统的架构设计 1、大规模跨节点专家并行 (EP) 2、计算-通信重叠 3、负载均衡 4、在线推理系统图 DeepSeek-V3/R1推理系统的架构设计 2025年3月…

compose multiplatform写一个简单的阅读器2

目录 解码全部换成mupdf 遇到的一些问题 相对上一篇文章修正的内容: 使用感受: 未来可能要完成的功能: 解码全部换成mupdf android里面的pdf解析,背景没有了,换了mupdf就正常了. 解码的代码统一,省的麻烦了. mupdf不只支持pdf,还支持epub/mobi这些,这在桌面端没有找到一…

通过返回的key值匹配字典中的value值

需求 页面中上面搜索项有获取字典枚举接口&#xff0c;table表格中也有根据key匹配字典中的value 方案一 需要做到的要求 这里上面下拉列表是一个组件获取的字典&#xff0c;下面也是通过字典匹配&#xff0c;所以尽量统一封装一个函数&#xff0c;每个组件保证最少变动tabl…

苹果CMS泛目录优化全攻略:局部URL随机化与数据无缝对接

引言 在当今的SEO优化领域&#xff0c;泛目录和站群策略已经成为提升网站权重和流量的重要手段。苹果CMS作为一款功能强大的内容管理系统&#xff0c;其新版框架为开发者提供了丰富的二次开发接口&#xff0c;使得泛目录和站群策略的实现变得更加灵活和高效aoshunseo。本文将详…

基于STM32的智能家居能源管理系统

1. 引言 传统家庭能源管理存在能耗监控粗放、设备联动不足等问题&#xff0c;难以适应绿色低碳发展需求。本文设计了一款基于STM32的智能家居能源管理系统&#xff0c;通过多源能耗监测、负荷预测与优化调度技术&#xff0c;实现家庭能源的精细化管理与智能优化&#xff0c;提…