rotate array_Array.rotate! Ruby中的示例方法

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544435.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【python】获取PC机公网IP并发送至邮箱

文章目录0.引言1.获取外网IP2.打开SMTP服务3.python发送邮件4.完整代码0.引言 \qquad之前一直使用Putty连接公司的PC机进行远程办公,苦于外网的IP地址不能固定下来,所以购买了内网穿透服务,免费版还会限速。后来转念一想,如果能定…

List 去重的 6 种方法,这个方法最完美!

作者 | 王磊来源 | Java中文社群(ID:javacn666)转载请联系授权(微信ID:GG_Stone)在日常的业务开发中,偶尔会遇到需要将 List 集合中的重复数据去除掉的场景。这个时候可能有同学会问&#xff1a…

Mongodb -(3) replica set+sharding

分片集搭建---何旭东目录分片集搭建...................................................................................................................... 1生态系统...............................................................................................…

electron 菜单栏_如何在Electron JS中添加任务栏图标菜单?

electron 菜单栏If you are new here, please consider checking out my recent articles on Electron JS including Tray Icons. 如果您是新来的,请考虑查看我最近关于Electron JS的文章, 包括托盘图标 。 In this tutorial, we will set up 2 menu it…

【逆强化学习-0】Introduction

文章目录专栏传送门0.引言1.逆强化学习发展历程2.需要准备的专栏传送门 0.简介 1.学徒学习 2.最大熵学习 0.引言 \qquad相比于深度学习,国内强化学习的教程并不是特别多,而相比强化学习,逆强化学习的教程可谓是少之又少。而本人想将整理到的资…

不知道Mysql排序的特性,加班到12点,认了认了!

小弟新写了一个功能,自测和测试环境测试都没问题,但在生产环境会出现偶发问题。于是,加班到12点一直排查问题,终于定位了的问题原因:Mysql Limit查询优化导致。现抽象出问题模型及解决方案,分析给大家&…

js中==与===的区别

2019独角兽企业重金招聘Python工程师标准>>> 1、对于string,number等基础类型,和是有区别的 1)不同类型间比较,之比较“转化成同一类型后的值”看“值”是否相等,如果类型不同,其结果就是不等 2&#xff09…

c语言中memcpy函数_带有示例的C中的memcpy()函数

c语言中memcpy函数memcpy()函数 (memcpy() function) memcpy() is a library function, which is declared in the “string.h” header file - it is used to copy a block of memory from one location to another (it can also be considered as to copy a string to anothe…

【逆强化学习-1】学徒学习(Apprenticeship Learning)

文章目录0.引言1.算法原理2.仿真环境3.运行4.补充(学徒学习深度Q网络)本文为逆强化学习系列第1篇,没有看过逆强化学习介绍的那篇的朋友,可以看一下:Inverse Reinforcement Learning-Introduction 传送门 0.引言 \qquad…

面试官:HashMap有几种遍历方法?推荐使用哪种?

作者 | 磊哥来源 | Java面试真题解析(ID:aimianshi666)转载请联系授权(微信ID:GG_Stone)HashMap 的遍历方法有很多种,不同的 JDK 版本有不同的写法,其中 JDK 8 就提供了 3 种 HashMa…

HTML 5 input placeholder 属性

<input placeholder"请先选择组织" type"text" value"" </input>placeholder 属性提供可描述输入字段预期值的提示信息&#xff08;hint&#xff09;。 该提示会在输入字段为空时显示&#xff0c;并会在字段获得焦点时消失。 注释&…

【逆强化学习-2】最大熵学习(Maximum Entropy Learning)

文章目录0.引言1.算法原理2.仿真0.引言 \qquad本文是逆强化学习系列的第2篇&#xff0c;其余博客传送门如下&#xff1a; 逆强化学习0-Introduction 逆强化学习1-学徒学习 \qquad最大熵学习是2008年出现的方法&#xff0c;原论文&#xff08;链接见【逆强化学习0】的博客&#…

uselocale_Java扫描仪useLocale()方法与示例

uselocale扫描器类useLocale()方法 (Scanner Class useLocale() method) useLocale() method is available in java.util package. useLocale()方法在java.util包中可用。 useLocale() method is used to use this Scanner locale to the given locale (lo). useLocale()方法用…

面试官又整新活,居然问我for循环用i++和++i哪个效率高?

前几天&#xff0c;一个小伙伴告诉我&#xff0c;他在面试的时候被面试官问了这么一个问题&#xff1a;在for循环中&#xff0c;到底应该用 i 还是 i &#xff1f;听到这&#xff0c;我感觉这面试官确实有点不按套路出牌了&#xff0c;放着好好的八股文不问&#xff0c;净整些幺…

UVa 988 - Many Paths, One Destination

称号&#xff1a;生命是非常多的选择。现在给你一些选择&#xff08;0~n-1&#xff09;&#xff0c;和其他选项后&#xff0c;分支数每一次选择&#xff0c;选择共求。 分析&#xff1a;dp&#xff0c;图论。假设一个状态也许是选择的数量0一个是&#xff0c;代表死亡&#xff…

Java PrintWriter close()方法与示例

PrintWriter类close()方法 (PrintWriter Class close() method) close() method is available in java.io package. close()方法在java.io包中可用。 close() method is used to close this stream and free all system resources linked with the stream. close()方法用于关闭…

pipedreader_Java PipedReader ready()方法与示例

pipedreaderPipedReader类ready()方法 (PipedReader Class ready() method) ready() method is available in java.io package. ready()方法在java.io包中可用。 ready() method is used to check whether this PipedReader stream is ready to be read or not. ready()方法用…

面试官:如何实现 List 集合去重?

作者 | 磊哥来源 | Java面试真题解析&#xff08;ID&#xff1a;aimianshi666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;本文已收录《Java常见面试题》系列&#xff0c;开源地址&#xff1a;https://gitee.com/mydb/interviewList 去重指的…

Windows重装Anaconda3失败解决方案【重装失败10来次首次成功的案例!】

文章目录0.环境1.原因2.解决方案0.环境 Win10 Anaconda3 2018版 python 3.7.1 注意&#xff01;此种情况只会在windows上发生&#xff0c;因为在linux上你只需要删除anaconda3整个文件夹&#xff0c;重新安装一定会成功&#xff01; 1.原因 Anaconda肯定是没有成功安装的&am…

java写入文件的几种方法分享

转自&#xff1a;http://www.jb51.net/article/47062.htm 一&#xff0c;FileWritter写入文件 FileWritter, 字符流写入字符到文件。默认情况下&#xff0c;它会使用新的内容取代所有现有的内容&#xff0c;然而&#xff0c;当指定一个true &#xff08;布尔&#xff09;值作为…