ruby array_在Ruby中使用Array.first和Array.last方法访问数组的第一个和最后一个元素...
ruby array
In Ruby, you have the privilege to access the first and last method of an array directly without putting some different logic for it with the help of Array.first and Array.last methods.
在Ruby中,您有特权直接访问数组的first和last方法,而无需借助Array.first和Array.last方法为其添加其他逻辑。
In case you want to get the first letter of the Array you may use Array.at(0) method because indexing starts with 0 or it can be directly done with Array.first but if you have to find the last element of the Array, you will have to first find the length of the Array then you will have to subtract the length by 1 and then print it with the help of Array.at(Array.count()-1) method. The process gets a little hectic. So, for making the task simpler, we have Array.last method which will directly return you the last element of the Array with the lesser lines of code.
如果要获取Array的第一个字母,则可以使用Array.at(0)方法,因为索引从0开始,或者可以直接用Array.first完成,但是如果必须找到Array的最后一个元素,您必须首先找到Array的长度,然后将其减去1,然后借助Array.at(Array.count()-1)方法进行打印。 这个过程有点忙。 因此,为了简化任务,我们提供了Array.last方法 ,该方法将直接使用较少的代码行直接返回Array的最后一个元素。
Now, let us see how both methods are implemented with their syntaxes and Ruby code.
现在,让我们看看这两种方法是如何使用其语法和Ruby代码实现的。
Syntax:
句法:
# first element Array.first # last element Array.last[/code]
The scenarios will be clearer to you when you will go through their examples. The first example belongs to Array.first and latter belongs to Array.last.
当您浏览它们的示例时,这些场景对您而言将更加清晰。 第一个示例属于Array.first ,后者属于Array.last 。
Example 1:
范例1:
# array declaration Adc = ['Includehelp.com','Ruby','C++','C#','Java','Python'] # Getting first element using Array.at() method puts "Array.at(0) method" puts "The first element of Array : #{Adc.at(0)}" # Getting first element using Array.first method puts "Array.first method" puts "The first element of Array : #{Adc.first}"[/code]
Output
输出量
Array.at(0) method The first element of Array : Includehelp.com Array.first method The first element of Array : Includehelp.com[/code]
Explanation:
说明:
In the above code, you can understand that we are retrieving the first element of the Array with the help of two methods, firstly with the help of Array.at() method and then with the help of Array.first method. Both methods are giving the desired output.
在上面的代码中,您可以理解,我们借助于两种方法来检索Array的第一个元素,首先是借助Array.at()方法 ,然后是Array.first方法 。 两种方法都可以提供所需的输出。
Example 2:
范例2:
# array declaration Adc = ['Includehelp.com','Ruby','C++','C#','Java','Python'] # Getting last element using Array.at() method puts "Array.at(Array.count()-1) method" puts "The first element of Array : #{Adc.at(Adc.count()-1)}" # Getting last element using Array.last method puts "Array.last method" puts "The first element of Array : #{Adc.last}"[/code]
Output
输出量
Array.at(Array.count()-1) method The first element of Array : Python Array.last method The first element of Array : Python[/code]
Explanation:
说明:
In the above code, you can observe that we are using two different methods for the completion of the same task. The first method is Array.at() along with Array.count(). We can achieve the same thing by avoiding the use of two different methods and adopting the Array.last method. Both ways are giving us the desired output. Array.last method is considered to be very effective for this purpose because it is defined to serve this purpose only.
在上面的代码中,您可以观察到我们正在使用两种不同的方法来完成同一任务。 第一个方法是Array.at()和Array.count() 。 通过避免使用两种不同的方法并采用Array.last方法,我们可以实现同一件事。 两种方式都可以为我们提供所需的输出。 Array.last方法被认为非常有效,因为它被定义为仅用于此目的。
ruby array
- 使用php自带函数删除数组的第一个元素和最后一个元素
- CSS中的第一个:first-letter 最后一个:last-child 第n个:nth-child 伪元素
- JavaScript使用pop方法移除数组最后一个元素用法实例
- array数组扩展方法二、-- 数组中的对象元素进行归类,参数值相同的对象放在一起,以数组第一个元素为准
- php删除数组最后一个元素和第一个元素,array_shift(),array_pop()的用法
- css中使用伪类:not(first-child)过滤不能排除第一个元素的解决方法
- 使用sortedArrayUsingSelector:方法排序数组中的元素
- 【程序35】 ArrayChange.java 题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
- php通过array_shift()函数移除数组第一个元素的方法
- awk命令的使用(三)获取数组的最后一个元素
- 使用javascript去掉数组中重复元素的方法
- List的All方法使用问题:我用List1的All方法来确保List1的所有每个数组元素中是否包含Arr1中的任意元素,这个部分很难实现。 因为,没有一个数组中是否包含另一个数组中的任意元素这个功能
- 【php数组函数序列】之array_slice() - 删除数组中的最后一个元素
- 将数组元素循环流动的问题(即数组第一个元素变成最后一个,后面的补上,如此循环几次)
- 利用Array的splice方法,删除数组的元素
- 详解JavaScript中数组和字符串的lastIndexOf()方法使用
- 使用泛型, 写一个为任意类型的动态数组添加元素的方法
- PHP取数组第一个元素的方法
- PHP使用array_merge重新排列数组下标的方法
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例