zemax微透鏡陣列示例
陣列反向! 方法 (Array reverse! Method)
In this article, we will study about Array.reverse! method. You all must be thinking the method must be doing something related to reversing certain elements as we have done in the case of Array.reverse! method. 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! 方法 。 大家都必須認為該方法必須像在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 destructive method where the changes created by this method would impact the actual order of the Self Array instance and these changes are permanent.
該方法是一個公共實例方法,為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: Kotlin
Array elements are:
["Kotlin", "Ruby", "php", "Javascript", "Html", "Python", "Java", "C++"]
Explanation:
說明:
In the above code, you can observe that we are reversing the contents of Array class instance with the help of Array.reverse! method. You can observe that after reversing the contents, the first element is "Kotlin" because this method is a destructive method and creates changes in the actual arrangements of contents in Array instance.
在上面的代碼中,您可以觀察到借助Array.reverse ,我們正在反轉Array類實例的內容! 方法 。 您可以觀察到,在反轉內容之后,第一個元素是“ Kotlin”,因為此方法是一種破壞性方法,會在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: 20
Array elements are:
[20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
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 after reversing the contents, the first element is 20 because this method is destructive and creates changes in the actual arrangements of contents in the Array instance.
在上面的代碼中,您可以觀察到該方法也適用于Integer Array,并且借助于Array.reverse ,我們正在反轉Array類實例的內容! 方法 。 您可以觀察到,在反轉內容之后,第一個元素為20,因為此方法具有破壞性,并會更改Array實例中內容的實際排列。
翻譯自: https://www.includehelp.com/ruby/array-reverse-inverse-method-with-example.aspx
zemax微透鏡陣列示例