array.reverse_Ruby中带有示例的Array.reverse方法

array.reverse

Array.reverse方法 (Array.reverse Method)

In this article, we will study about Array.reverse method. You all must be thinking the method must be doing something which is related to reversing certain elements. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

在本文中,我们将研究Array.reverse方法 。 大家都必须认为该方法必须执行与反转某些元素有关的操作。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

Method description:

方法说明:

This method is a public instance method and defined for the Array class in Ruby's library. This method works in a way that reverses the content or objects present inside the Array instances. It traverses the Array elements in the way that the last element is considered to be first and the first object is considered to be the last element of the object of Array instance. Array.reverse is a non-destructive method where the changes created by this method would not impact the actual order of the Self Array instance.

该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法的工作方式是反转Array实例中存在的内容或对象。 它以以下方式遍历Array元素:将最后一个元素视为第一个元素,将第一个对象视为Array实例对象的最后一个元素。 Array.reverse是一种非破坏性方法,其中此方法创建的更改不会影响Self Array实例的实际顺序。

Syntax:

句法:

    array_instance.reverse -> new_array

Argument(s) required:

所需参数:

This method does not require any argument. Reversing the Array instance does not require anything rather than the Array instance itself.

此方法不需要任何参数。 反转Array实例不需要什么,只需要Array实例本身即可。

Example 1:

范例1:

=begin
  Ruby program to demonstrate reverse method
=end

# array declaration
lang = ["C++","Java","Python","Html","Javascript","php","Ruby","Kotlin"]

puts "Array reverse implementation."
print lang.reverse
puts ""

puts "The first element of the Array is: #{lang[0]}"
puts "Array elements are:"
print lang

Output

输出量

Array reverse implementation.
["Kotlin", "Ruby", "php", "Javascript", "Html", "Python", "Java", "C++"]
The first element of the Array is: C++
Array elements are:
["C++", "Java", "Python", "Html", "Javascript", "php", "Ruby", "Kotlin"]

Explanation:

说明:

In the above code, you can observe that we are reversing the contents of the Array class instance with the help of the Array.reverse method. You can observe that even after reversing the contents, the first element is still "C++" because this method is a non-destructive method and does not create any changes in the actual arrangements of contents in Array instance.

在上面的代码中,您可以观察到借助于Array.reverse方法 ,我们正在反转Array类实例的内容。 您可以观察到,即使在反转内容之后,第一个元素仍然是“ C ++”,因为此方法是非破坏性的方法,并且不会在Array实例中对内容的实际排列进行任何更改。

Example 2:

范例2:

=begin
  Ruby program to demonstrate reverse method
=end

# array declaration
table = [2,4,6,8,10,12,14,16,18,20]

puts "Array reverse implementation"
print table.reverse
puts ""

puts "The first element of the Array is: #{table.first}"
puts "Array elements are:"
print table

Output

输出量

Array reverse implementation
[20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
The first element of the Array is: 2
Array elements are:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Explanation:

说明:

In the above code, you can observe that this method works on Integer Array as well and we are reversing the contents of Array class instance with the help of Array.reverse method. You can observe that even after reversing the contents, the first element is still 2 because this method is non-destructive and does not create any changes in the actual arrangements of contents in the Array instance.

在上面的代码中,您可以观察到该方法也适用于Integer Array,并且借助于Array.reverse方法 ,我们正在反转Array类实例的内容。 您可以观察到,即使在反转内容之后,第一个元素仍为2,因为此方法是非破坏性的,并且不会在Array实例中对内容的实际排列进行任何更改。

翻译自: https://www.includehelp.com/ruby/array-reverse-method-with-example.aspx

array.reverse

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值