ESS多版本 【TA】【ESS】04 数组

TAAAAAAA

天王
管理成员
2024/06/16
200
3
27
1,210
4: Arrays 数组

在最开始,先来解决一下前一章留的关于return的作业:
Ruby:
def seconds_lived(age)
  return 0 if age < 0 # 早返回
  return "Nobody has ever lived longer than 122 years!" if age > 122 # 早返回
  60 * 60 * 24 * 365 * age # 隐式返回
end

# 那么,你学废了吗?

那么我们开始数组部分的学习。

Arrays are essentially lists of multiple values. Just like we have Fixnum, String, Float, FalseClass, TrueClass, and NilClass, Array is also a data type. An array is an ordered list of values which are usually called elements or entries. Each element has a certain index with which they’re referred to, for instance, Element 0, Element 1, Element 2 - that’s right, indexes start at 0 instead of 1. An array is created by writing down square brackets, [], and inserting your values while separating them with commas. Here’s a basic example of what an array could look like:

数组本质上就是一系列数据的一个列表。
当然,数组本身也是一种数据类型。
数组中的每一个数据,称为数组中的一个元素。
每一个元素,有其对应的一个序号。
比如说,第0号元素、第1号元素和第2号元素,等等。
这个时候你会发现,我们是从0开始数的,也就是说,数组中的第1个元素实际上是第0号元素、第2个元素实际上是第1号元素,等等,以此类推。
我们使用中括号[]来新创建数组,数组里面可以有一个元素,也可以有多个元素,也可以没有任何元素,当数组中没有任何元素时,那么此时这个数组就是一个空数组;当数组中有多个元素时,我们使用逗号来把它们分隔开。
我们来看下面的这个例子:

Ruby:
my_array = [7, 3, 34]

At index 0, the first element of the array, we have 7.
At index 1, the second element of the array, we have 3.
At index 2, the third and last element of the array, we have 34.

在这个例子中,我们新创建了一个叫做my_array(我的数组)的数组,这个数组包含3个元素,分别是7、3和34。
其中,第0号元素是7,第1号元素是3,第2号元素是34。

This means that the size of the array is 3, but the last index is actually 2.
To retrieve an element of the array at a certain index, you would write down [index] after the array.

也就是说,对于my_array这个数组来说,因为数组中有3个元素,所以数组的大小是3,但是,因为我们数序号是从0开始数的,所以这个数组的最后一个序号是2。
我们使用xxx[yyy]这样的格式来获取数组中的元素,其中xxx就是数组名,yyy就是对应的元素在数组中的序号。
我们来看下面这个例子:

Ruby:
my_array = [1,2,3]
print my_array[0] #=> 1
print my_array[1] #=> 2
print my_array[2] #=> 3

(You can have as many spaces as you want when creating an array, you can either have no spaces at all or you can have tons)

在上面的例子中,我们通过my_array[0]来获取my_array中的第1个元素,通过my_array[1]来获取my_array中的第2个元素,通过my_array[2]来获取my_array中的第3个元素。

You can also change an element just like you would assign to a normal variable. Where my_array = 7 would change the whole array to 7, my_array[1] = 7 would only change the second element (index 1) to 7.

在获取到序号对应的元素之后,我们也可以为这个元素指定新的值,我们来看下面这个例子:

Ruby:
y_array = [1,2,3]
print my_array #=> [1,2,3]

my_array[1] = 7
# 获取到第1号元素,接着为其重新赋值为7。
print my_array #=> [1,7,3]
# 现在my_array就不再是[1, 2, 3]了,而是[1, 7, 3]。

my_array = 7
print my_array #=> 7
# 这个时候,my_array这个变量不再是数组了,而是一个7,
# 因为我们为这个变量重新赋值了。
 
最后编辑:

TAAAAAAA

天王
管理成员
2024/06/16
200
3
27
1,210
Different data types inside arrays 在数组中的不同数据类型的元素

You can have all sorts of data types inside an array.
Unlike in statically typed languages like C and C#, an array does not accept just one data type. You can have floats, integers, strings, array, hashes, custom objects, booleans, whatever you want, all in one array.

数组中的元素可以是任意的数据类型,我们来看下面的这个例子:

Ruby:
a = ["hello", 3.14, ["this", "is", "an", "array", ["in", "an", ["array"]]], ["i", "bet", "you", "like", "that"]]
# 在a这个数组中,有字符串、浮点数和数组(甚至数组中还有数组)。
print a[0] #=> "hello"
print a[1] #=> 3.14
print a[2] #=> ["this", "is", "an", "array", ["in", "an", ["array"]]]
print a[2][0] #=> "this"
print a[2][1] #=> "is"
print a[2][2] #=> "an"
print a[2][3] #=> "array"
print a[2][4] #=> ["in", "an", ["array"]]
print a[2][4][0] #=> "in"
print a[2][4][1] #=> "an"
print a[2][4][2] #=> ["array"]
print a[2][4][2][0] #=> "array"
print a[3] #=> ["i", "bet", "you","like", "that"]
 
最后编辑:

TAAAAAAA

天王
管理成员
2024/06/16
200
3
27
1,210
Going outside of bounds 突破界限

If you choose an index that would be larger than the highest index currently in an array, you’ll be given nil.

当你想要使用xxx[yyy]这样的格式,来获取一个数组中不存在的元素时,程序并不会报错,而是会返回nil。

In various other languages, this would raise an ArgumentOutOfRange error.

我们来看下面的这个例子:

Ruby:
a = [1,2,3]
print a #=> [1,2,3]
print a[0] #=> 1
print a[1] #=> 2
print a[2] #=> 3
print a[3] #=> nil
print a[4] #=> nil
print a[3242] #=> nil
# 因为第3号、4号和3242号元素并不存在,所以返回了nil。

You can actually assign variables when you’re out of bounds though.

你可以为不存在的元素进行赋值,请看下面的例子:

Ruby:
a = [1,2,3]
print a #=> [1,2,3]
print a[0] #=> 1
print a[1] #=> 2
print a[2] #=> 3
print a[7] #=> nil
print a #=> [1,2,3]
# Everything is like normal so far

a[7] = 8
print a #=> [1,2,3,nil,nil,nil,nil,8]
# 通过为第7号元素赋值8,程序会为数组用nil补齐中间缺失的元素。

The array is filled up with nil up to the index you’re assigning to. In this case, the highest index was 2, and we assigned index 7. That means index 3, 4, 5 and 6 are empty, and those are set to nil.

Another neat trick you can do is to turn something like this:

我们来看数组的另一种用法:

Ruby:
gender = 1
if gender == 0
  life_expectancy = 79
elsif gender == 1
  life_expectancy = 80
end
print life_expectancy #=> 80

...into this:

Ruby:
gender = 1
life_expectancy = [79,80][gender] # gender is 1, so [79,80][1] #=> 1
print life_expectancy #=> 80
# 将79和80放到一个数组中,用gender来作为序号来获取数组中的元素。
 

TAAAAAAA

天王
管理成员
2024/06/16
200
3
27
1,210
Methods on arrays 数组方法

A data type can have a collection of methods. Just like we created methods in the main scope in the tutorial on methods, you can also have methods on data types (such as Array). These methods will be available to everything of that data type. Exactly how this works and what this is will be covered in tutorials 6 and 7 (classes).

有一些专门用来处理数组的方法,我们来看下面的一些例子:

size: Returns the size of the array. (synonym: length; does the exact same.)

size(大小)用来获取数组的大小,除了size之外,你也可以使用length(长度),它们是一样的。
请看下面的例子:

Ruby:
a = ["one","two","three","four"]
print a.size #=> 4

可以发现,我们是在数组的名字后面,加一个点“.”,这个其实是英文的句号,接着再写size来得到a的大小的。
也就是说,我们是对a数组调用了size方法,所以,“.”其实就是调用的意思。

push(value): Adds value to the array.

push(xxx)(推)用来在数组后面添加新的元素,xxx就是新添加的元素的值,请看下面的例子:

Ruby:
a = ["one","two","three"]
print a #=> ["one","two","three"]

a.push("four")
print a #=> ["one","two","three","four"]

delete_at(idx): Deletes the element at idx.

delete_at(xxx)(在...删除)用来删除数组中对应序号的元素,xxx就是序号,请看下面的例子:

Ruby:
a = ["one","two","three","four"]
print a #=> ["one","two","three","four"]

a.delete_at(1)
print a #=> ["one","three","four"]

insert(idx, value): Inserts value at the idx index, but doesn’t overwrite anything. The other elements just move one over.

insert(xxx, yyy)(插入)用来在数组中间添加元素,其中xxx是新插入的元素的序号,yyy是新插入的元素的值。
插入元素之后,原本位于xxx号以及之后的元素,会向后移。
请看下面的例子:

Ruby:
a = ["one","three","four"]
print a #=> ["one","three","four"]

a.insert(1, "two")
print a #=> ["one","two","three","four"]

delete(value): Deletes every occurance of the value.

delete(xxx)(删除)用来删除数组中的所有值是xxx的元素,请看下面的这个例子:

Ruby:
a = ["one","two","three","one","two","three","one","two","three"]
print a #=> ["one","two","three","one","two","three","one","two","three"]

a.delete("two")
print a #=> ["one","three","one","three","one","three"]

include?(value): Returns true if the value exists in the array, false if not.

include?(xxx)(包括吗?)用来检查数组中是否含有值是xxx的元素,这个方法会返回true或者false,请看下面的这个例子:

Ruby:
a = ["one","two","three"]
print a.include?("three") #=> true
print a.include?("four") #=> false

本章完。
 
最后编辑:

在线成员

论坛统计

主题
474
消息
2,137
成员
2,909
最新成员
小灵喵~