array.unshift_Ruby中带有示例的Array.unshift()方法

array.unshift

Array.unshift()方法 (Array.unshift() Method)

In this article, we will study about Array.unshift() Method. You all must be thinking the method must be doing something which is related to unshifting of objects in the Array instance. 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.unshift()方法 。 你们都必须认为该方法必须执行与Array实例中对象的移位相关的操作。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

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 affixes the object which is provided with the method at the time of its invocation to the front of the self Array object. It simply means that the object will be shifted to the 0th index and the indices of rest objects are incremented by one. You can provide more than one argument to the method at the time of its invocation. This method is one of the examples of destructive methods where the changes created by the method are permanent. There is no non-destructive version of this method.

该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法的工作方式是在调用方法时将随方法提供的对象附加到self Array对象的前面。 这仅表示该对象将移至第0个索引,其余对象的索引将增加1。 调用方法时,可以为该方法提供多个参数。 此方法是破坏性方法的示例之一,该方法所创建的更改是永久性的。 没有此方法的非破坏性版本。

Syntax:

句法:

    array_instance.unshift(object) -> array

Argument(s) required:

所需参数:

This method can take n number of objects. You can even provide an Array instance as the argument.

此方法可以使用n个对象。 您甚至可以提供一个Array实例作为参数。

Example 1:

范例1:

=begin
  Ruby program to demonstrate unshift method
=end

# array declaration
table = [2,4,8,10,12,134,160,180,200,344]

puts "Array unshift implementation"

puts "Enter the number of objects you want to add:"
num = gets.chomp.to_i

for i in 1..num
	puts "Enter the object:"
	ele = gets.chomp
	table.unshift(ele)
end

puts "The final Array instance: #{table}"

Output

输出量

Array unshift implementation
Enter the number of objects you want to add:
 3
Enter the object:
 Hrithik
Enter the object:
 Amisha
Enter the object:
 Satyam
The final Array instance: ["Satyam", "Amisha", "Hrithik", 2, 4, 8, 10, 12, 134, 160, 180, 200, 344]

Explanation:

说明:

In the above code, you can observe that we are adding elements in the Array instance with the help of the Array.unshift() method. In the output, you can see that the objects which are given by the user are stored from 0th index. The previously present elements are moved upward.

在上面的代码中,您可以观察到,借助于Array.unshift()方法 ,我们正在Array实例中添加元素。 在输出中,您可以看到用户指定的对象是从 0 索引开始存储的。 先前存在的元素向上移动。

Example 2:

范例2:

=begin
  Ruby program to demonstrate unshift method
=end

# array declaration
table = [2,4,8,10,12,134,160,180,200,344]

puts "Array unshift implementation"
Name = ["Ayush","Saksham","Nikhil"]

table.unshift(Name)

puts "The final Array instance: #{table}"

Output

输出量

Array unshift implementation
The final Array instance: [["Ayush", "Saksham", "Nikhil"], 2, 4, 8, 10, 12, 134, 160, 180, 200, 344]

Explanation:

说明:

In the above code, you can observe that we have prepended an Array instance in the self Array. Now, it has become a subarray of the self Array which is stored at the 0th index of the self Array.

在上面的代码中,您可以观察到我们已经在self Array中添加了Array实例。 现在,它已成为self Array的子​​数组,存储在self Array的第0 索引处。

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

array.unshift

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值