4: Arrays 数组
在最开始,先来解决一下前一章留的关于return的作业:
那么我们开始数组部分的学习。
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号元素,等等,以此类推。
我们使用中括号[]来新创建数组,数组里面可以有一个元素,也可以有多个元素,也可以没有任何元素,当数组中没有任何元素时,那么此时这个数组就是一个空数组;当数组中有多个元素时,我们使用逗号来把它们分隔开。
我们来看下面的这个例子:
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就是对应的元素在数组中的序号。
我们来看下面这个例子:
(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.
在获取到序号对应的元素之后,我们也可以为这个元素指定新的值,我们来看下面这个例子:
在最开始,先来解决一下前一章留的关于return的作业:
那么我们开始数组部分的学习。
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号元素,等等,以此类推。
我们使用中括号[]来新创建数组,数组里面可以有一个元素,也可以有多个元素,也可以没有任何元素,当数组中没有任何元素时,那么此时这个数组就是一个空数组;当数组中有多个元素时,我们使用逗号来把它们分隔开。
我们来看下面的这个例子:
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就是对应的元素在数组中的序号。
我们来看下面这个例子:
(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.
在获取到序号对应的元素之后,我们也可以为这个元素指定新的值,我们来看下面这个例子: