obj.val 非数组_在Ruby中使用Array.new(size,obj)创建数组

obj.val 非数组

In the previous article, we have learnt how we can declare an Array class instance with the help of Array.[](*args) 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。[](* args)方法的帮助下声明Array类实例? 您还可以注意到,在编写用于演示所有这些方法的程序代码中,它们具有使用常规方法声明的Array实例,例如,

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

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

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

    array_name = Array.[](*args)

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 Array.new(size, obj) method?

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

Method description:

方法说明:

This method is a public class method. This method accepts two arguments, the first one is the size of the object you want to create and the second one is the element you want to store in the Array. It will replicate the element and create the size copies of that element and store it in your Array object. You should go for this method if you want to store the same element for more than one time.

此方法是公共类方法。 此方法接受两个参数,第一个是要创建的对象的大小,第二个是要存储在Array中的元素。 它将复制元素并创建该元素的大小副本,并将其存储在Array对象中。 如果要多次存储同一元素,则应使用此方法。

Syntax:

句法:

    array_name = Array.new(size = 0, obj = nil)

Parameter(s):

参数:

Arguments play a very important role in this method. This method will take two arguments, the first one is the size and the second one is the element. If you don't provide any arguments, it will result in an empty Array with no elements in it.

在这种方法中,参数起着非常重要的作用。 此方法将有两个参数,第一个是尺寸,第二个是元素。 如果不提供任何参数,它将导致一个空数组,其中没有任何元素。

Example 1:

范例1:

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

# array declaration
arr = Array.new(size = 5, obj = "Hrithik")

# 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:
Hrithik
Hrithik
Hrithik
Hrithik
Hrithik
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(size, obj) method. This method creates a copy of the same element for size times. 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(size,obj)方法的实现 。 此方法为大小时间创建相同元素的副本。 您还可以观察到,如果我们没有为该方法提供参数,则Array会生成一个空Array,并且借助Array.count method可以证明这一点。

Example 2:

范例2:

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

# input element
puts "Enter the element you want to keep in the Array instance"
ele1 = gets.chomp

# input array size
puts "Enter the size of Array"
siz = gets.chomp.to_i

# creating array
arr = Array.new(size = siz, obj = ele1)

# printing array elements
puts "Array elements are:"
puts arr

Output

输出量

RUN 1:
Enter the element you want to keep in the Array instance
IncludeHelp
Enter the size of Array
5
Array elements are:
IncludeHelp
IncludeHelp
IncludeHelp
IncludeHelp
IncludeHelp

RUN 2:
Enter the element you want to keep in the Array instance
100
Enter the size of Array
3
Array elements are:
100
100
100

Explanation:

说明:

In the above code, you can observe that we are taking two inputs from the user first one is the size of Array and the second one is the element we want to store in it. You can see from the output that our Array instance is containing that element in size numbers.

在上面的代码中,您可以观察到我们从用户那里获取了两个输入,第一个是Array的大小,第二个是我们要存储在其中的元素。 从输出中可以看到,我们的Array实例包含该元素的大小编号。

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

obj.val 非数组

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值