ruby array_在Ruby中使用Array.new(Array_object)创建数组

ruby array

In the previous article, we have learned how we can declare an Array class instance with the help of Array.new(size, obj) method? You can also notice that in the program codes written to demonstrate all those methods are having Array instances declared with the conventional method such as,

在上一篇文章中,我们学习了如何在Array.new(size,obj)方法的帮助下声明Array类实例? 您还可以注意到,在编写用于演示所有这些方法的程序代码中,它们具有使用常规方法声明的Array实例,例如,

    array_name = ['ele1', 'ele2', 'ele3', ..., 'eleN']

Now, after reading the previous articles, we have learnt to declare an Array class object in the following ways as well,

现在,在阅读了之前的文章之后 ,我们还学习了通过以下方式声明Array类对象:

    array_name = Array.[](*args)
    array_name = Array.new(size = 0, obj = nil)

The above is the way we have used it in the last article. In the upcoming articles, you will learn the different ways through which we can declare an Array instance. Well, in this article, we will see how we can declare an Array object with the help of the Array.new(Array_object) method.

以上是我们在上一篇文章中使用它的方式。 在接下来的文章中,您将学习声明数组实例的不同方法。 好吧,在本文中,我们将看到如何在Array.new(Array_object)方法的帮助下声明Array对象。

Method description:

方法说明:

This method is a public class method. This method is used when you want to create an Array instance with the help of an existing Array object. If you are creating a new Array with the help of the previous Array, this does not mean that the reference of both the Arrays will be the same. They will be different.

此方法是公共类方法。 当您想借助现有Array对象创建Array实例时,使用此方法。 如果要在先前的阵列的帮助下创建新的阵列,这并不意味着两个阵列的引用都将相同。 他们会有所不同。

Syntax:

句法:

    array_name = Array.new(Array_object)

Parameter(s):

参数:

Arguments play a very important role in this method. This method will take a previously defined Array as an argument. If you don’t provide any argument, it will result in an empty Array with no elements in it.

在这种方法中,参数起着非常重要的作用。 该方法将先前定义的Array作为参数。 如果不提供任何参数,它将导致一个空数组,其中没有任何元素。

Example 1:

范例1:

=begin
    Ruby program to demonstrate Array.new(Array)
=end

# array declaration
names = ['Sanjay','Ram','Shyam','Harish']

# creating array from array object
arr = Array.new(names)

# printing array elements
puts "Elements of \'arr\' are:"
puts arr

# creating an empty array
arr1 = Array.new()
puts "Number of elements present in \'arr1\' are: #{arr1.count}"

Output

输出量

Elements of 'arr' are:
Sanjay
Ram
Shyam
Harish
Number of elements present in 'arr1' are: 0

Explanation:

说明:

With the help of the above code, you can easily understand the implementation of the Array.new(Array) method. First, we have defined an Array instance named as ‘Name’ and we are passing that instance inside the new Array instance. You can also observe that if we are not giving parameters to the method, then the Array results into an empty Array and we have shown that with the help of Array.count method.

借助以上代码,您可以轻松了解Array.new(Array)方法的实现 。 首先,我们定义了一个名为“名称”的数组实例,并将该实例传递到新的数组实例中。 您还可以观察到,如果我们没有为该方法提供参数,则Array会生成一个空Array,并且借助Array.count method可以证明这一点。

Example 2:

范例2:

=begin
    Ruby program to demonstrate Array.new(Array)
=end

# input array elements
puts "Enter the first element you want to keep in the Array instance"
ele1 = gets.chomp
puts "Enter the second element you want to keep in the Array instance"
ele2 = gets.chomp
puts "Enter the third element you want to keep in the Array instance"
ele3 = gets.chomp
puts "Enter the fourth element you want to keep in the Array instance"
ele4 = gets.chomp

# creating array with user input elements
arr = [ele1,ele2,ele3,ele4]

# creating array from array objects
arr_new = Array.new(arr)
puts "Array elements are:"
puts arr

Output

输出量

Enter the first element you want to keep in the Array instance
Hrithik 
Enter the second element you want to keep in the Array instance 
B.Tech
Enter the third element you want to keep in the Array instance
Graphics Era University 
Enter the fourth element you want to keep in the Array instance 
India 
Array elements are: 
Hrithik 
B.Tech
Graphics Era University 
India 

Explanation:

说明:

In the above code, you can observe that we are taking multiple inputs from the user and storing them inside an Array and then we are passing that Array into new Array.

在上面的代码中,您可以观察到我们正在从用户那里获取多个输入,并将它们存储在Array中,然后将其传递给新的Array。

Example 3:

范例3:

=begin
    Ruby program to demonstrate the 
    reference of two arrays
=end

# array declaration
names = ['Sanjay','Ram','Shyam','Harish']

# creating array from array object
arr = Array.new(names)

# checking whether 'names' and 'arr' are equal or not
puts names.equal?arr

Output

输出量

false

Explanation:

说明:

In the above code, you can observe that when we are equating the references of both the Arrays, we are getting the result as "false" which simply means that the reference of both the Arrays are different even if the second Array instance is created with the help of first Array instance.

在上面的代码中,您可以观察到,当我们将两个Array的引用均等时,我们得到的结果为“ false” ,这仅意味着即使第二个Array实例是使用以下方式创建的,两个Array的引用也不同。在第一个Array实例的帮助下。

翻译自: https://www.includehelp.com/ruby/creating-array-with-array-new-array_object.aspx

ruby array

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值