您的位置:首页 > 编程语言 > Ruby

ruby array_在Ruby中使用Array.first和Array.last方法访问数组的第一个和最后一个元素...

2020-08-03 10:49 1671 查看

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方法被认为非常有效,因为它被定义为仅用于此目的。

翻译自: https://www.includehelp.com/ruby/accessing-first-and-last-element-of-an-array-using-array-first-and-array-last-methods.aspx

ruby array

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐