本文共 3772 字,大约阅读时间需要 12 分钟。
obj.val 非数组
In the previous article, we have learnt 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实例,例如,
array_name = ['ele1', 'ele2', 'ele3', ..., 'eleN']
Now, after reading the , 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 declarationarr = Array.new(size = 5, obj = "Hrithik")# printing array elementsputs "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:HrithikHrithikHrithikHrithikHrithikNumber 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 elementputs "Enter the element you want to keep in the Array instance"ele1 = gets.chomp# input array sizeputs "Enter the size of Array"siz = gets.chomp.to_i# creating arrayarr = Array.new(size = siz, obj = ele1)# printing array elementsputs "Array elements are:"puts arr
Output
输出量
RUN 1:Enter the element you want to keep in the Array instanceIncludeHelpEnter the size of Array5Array elements are:IncludeHelpIncludeHelpIncludeHelpIncludeHelpIncludeHelpRUN 2:Enter the element you want to keep in the Array instance100Enter the size of Array3Array elements are:100100100
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实例包含该元素的大小编号。
翻译自:
obj.val 非数组
转载地址:http://zytzd.baihongyu.com/