rotate array

Array.rotate! 方法 (Array.rotate! Method)

In this article, we will study about Array.rotate! method. You all must be thinking the method must be doing something which is related to rotating 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.rotate! 方法 。 你们都必须认为该方法必须执行与旋转某些元素有关的操作。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

Method description:

方法说明:

This method is a public instance method and defined for the Array class in Ruby's library. This method works in such a way that it rotates the content or objects present inside the Array instances. It rotates the Array elements in the way that the second element is considered to be first and the last object is considered to be the first element of the object of Array class. If you pass any negative integer inside the method then the rotation is done in the opposite direction. Array.rotate! is a destructive method where the changes created by this method would create an impact on the actual order of the Self Array instance and these changes are permanent.

该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法以旋转Array实例内部存在的内容或对象的方式工作。 它以以下方式旋转Array元素:将第二个元素视为第一个元素,将最后一个对象视为Array类的对象的第一个元素。 如果在方法内部传递任何负整数,则旋转方向相反。 Array.rotate! 是一种破坏性方法 ,其中此方法创建的更改将对Self Array实例的实际顺序产生影响,并且这些更改是永久的。

Syntax:

句法:

    array_instance.rotate! -> new_array
or
array_instance.rotate!(count) -> new_array

Argument(s) required:

所需参数:

This method does take one argument and that argument decides from which index the rotation is going to be held.

此方法确实采用一个参数,并且该参数决定将从哪个索引中保留旋转。

Example 1:

范例1:

=begin
Ruby program to demonstrate rotate! method
=end
# array declaration
lang = ["C++","Java","Python","Html","Javascript","php","Ruby","Kotlin"]
puts "Array rotate! implementation."
print lang.rotate!
puts ""
puts "The first element of the Array is: #{lang[0]}"
puts "Array elements are:"
print lang

Output

输出量

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

Explanation:

说明:

In the above code, you can observe that we are rotating the contents of Array class instance with the help of Array.rotate! method. You can observe that after rotating the contents, the first element is "Java" and the last element is "C++". Due to the fact that this method is a destructive method, it is creating an impact on the actual arrangements of elements in the Array instance.

在上面的代码中,您可以观察到我们正在借助Array.rotate旋转Array类实例的内容! 方法 。 您可以观察到旋转内容之后,第一个元素是“ Java” ,最后一个元素是“ C ++” 。 由于此方法是一种破坏性方法,因此它对Array实例中元素的实际排列产生了影响。

Example 2:

范例2:

=begin
Ruby program to demonstrate rotate! method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array rotate! implementation"
print table.rotate!(-2)
puts ""
puts "The first element of the Array is: #{table.first}"
puts "Array elements are:"
print table

Output

输出量

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

Explanation:

说明:

In the above code, you can observe that this method works on Integer Array as well and we are rotating the contents of Array class instance with the help of Array.rotate method. Since we are passing a negative integer inside the method, the rotation is started from the back end side of the Array instance and the 2nd element from the last. You can observe that after rotating the contents, the first element is 18 because this method is destructive and creates changes in the actual arrangements of contents in the Array instance.

在上面的代码中,您可以观察到该方法也适用于Integer Array,并且借助于Array.rotate方法 ,我们正在旋转Array类实例的内容 。 由于我们在方法内部传递了一个负整数,因此旋转从Array实例的后端开始,而第二个元素从最后一个开始。 您可以观察到,在旋转内容之后,第一个元素为18,因为此方法具有破坏性,并且会在Array实例中更改内容的实际排列。

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

rotate array

rotate array_Array.rotate! Ruby中的示例方法相关推荐

  1. ruby array_Array.select! Ruby中的示例方法

    ruby array Array.select! 方法 (Array.select! Method) In this article, we will study about Array.select ...

  2. zemax微透镜阵列示例_阵列反向! Ruby中的示例方法

    zemax微透镜阵列示例 阵列反向! 方法 (Array reverse! Method) In this article, we will study about Array.reverse! me ...

  3. as_hash ruby_Hash.merge(other_hash)方法与Ruby中的示例

    as_hash ruby Hash.merge(other_hash)方法 (Hash.merge(other_hash) Method) In this article, we will study ...

  4. Ruby中带有示例的Hash.key?(value)方法

    Hash.key?(value)方法 (Hash.key?(value) Method) In this article, we will study about Hash.key?(value) M ...

  5. 如何在Ruby中使用数组方法

    介绍 (Introduction) Arrays let you represent lists of data in your programs. Once you have data in an ...

  6. ruby中的回调方法和钩子方法

    在ruby中,当某些特定的事件发生时,将调用回调方法和钩子方法.事件有如下几种: 调用一个不存在的对象方法 类混含一个模块 定义类的子类 给类添加一个实例方法 给对象添加一个单例方法 引用一个不存在的 ...

  7. ruby hash方法_Hash.fetch()方法以及Ruby中的示例

    ruby hash方法 Hash.fetch()方法 (Hash.fetch() Method) In this article, we will study about Hash.fetch() M ...

  8. ruby array_Ruby中带有示例的Array.cycle()方法

    ruby array Array.cycle()方法 (Array.cycle() Method) In this article, we will study about Array.cycle() ...

  9. ruby array_Ruby中带有示例的Array.shuffle方法

    ruby array Array.shuffle方法 (Array.shuffle Method) In this article, we will study about Array.shuffle ...

最新文章

  1. Qt中不规则窗体和部件的实现
  2. 【C 语言】内存四区原理 ( 栈内存与堆内存对比示例 | 函数返回的堆内存指针 | 函数返回的栈内存指针 )
  3. 04.Java网络编程(转载)
  4. 有逼格的产品经理的工作台长啥样?
  5. 导出真实表格显示列数不能超过256_平均月薪真有6万5?说说我所知道的金融人真实薪酬...
  6. boost:验证Boost的BOOST_CLASS_REQUIRE宏
  7. 前端笔记-JavaScript中放json数组要注意的地方(构造灵活的echarts)
  8. SQL Server商业智能功能–创建简单的OLAP多维数据集
  9. 前端每周清单第 39 期: OpenAI 与 gRPC, Gitlab 的 Vue 年度总结,GraphQL 技术栈漫游
  10. 国际标准战争的技术真相
  11. 聊聊flink TaskManager的managed memory
  12. 利用 eutils 实现自动下载序列文件(python实现)
  13. 自己搭建RTMP流媒体服务器
  14. 图像金字塔实现图像缩放_一个简单的VueJS组件,用于图像放大/产品缩放
  15. 我与小娜(20):去LIGO,探秘光子接力赛
  16. 【蓝桥】 历届试题 分考场(DFS,回溯,剪枝,无向图染色问题)
  17. java eden space_《深入理解Java虚拟机》(六)堆内存使用分析,垃圾收集器 GC 日志解读...
  18. big java 中文版_Big Faceless Java PDF Library
  19. ArcGIS基础:计算地球椭球表面面积
  20. API网关BODY传输的配置和调用详解

热门文章

  1. mysql执行计划性能_MySQL SQL性能分析Explain执行计划
  2. html高度没有滚动条,Div扩展了页面高度,但没有滚动条
  3. Apache-Flink深度解析-DataStream-Connectors之Kafka
  4. HTML5中本地储存概念是什么,什么优点 ,与cookie有什么区别?
  5. 「作文素材详解」写作必知篇:语言优美不是作文第一要求
  6. 一点一点看JDK源码(五)java.util.ArrayList 后篇之removeIf与Predicate
  7. Oracle 10g 高级安装图文教程(二)
  8. 上手Caffe(一)
  9. MongoDB中关于64位整型存储解决方案
  10. RDataMining系列:Chapter 4 Decision Trees --决策树实现,未完待续