obj.val 非数组_在Ruby中使用Array.new(size,obj)创建数组
obj.val 非數(shù)組
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,
在上一篇文章中,我們學(xué)習(xí)了如何在Array。[](* args)方法的幫助下聲明Array類(lèi)實(shí)例? 您還可以注意到,在編寫(xiě)用于演示所有這些方法的程序代碼中,它們具有使用常規(guī)方法聲明的Array實(shí)例,例如,
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,
現(xiàn)在,在閱讀了上一篇文章之后 ,我們還學(xué)習(xí)了如何通過(guò)以下方式聲明Array類(lèi)對(duì)象:
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?
以上是我們?cè)谏弦黄恼轮惺褂盟姆绞健?在接下來(lái)的文章中,您將學(xué)習(xí)聲明數(shù)組實(shí)例的不同方法。 好吧,在本文中,我們將看到如何借助Array.new(size,obj)方法聲明一個(gè)Array對(duì)象?
Method description:
方法說(shuō)明:
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.
此方法是公共類(lèi)方法。 此方法接受兩個(gè)參數(shù),第一個(gè)是要?jiǎng)?chuàng)建的對(duì)象的大小,第二個(gè)是要存儲(chǔ)在Array中的元素。 它將復(fù)制元素并創(chuàng)建該元素的大小副本,并將其存儲(chǔ)在Array對(duì)象中。 如果要多次存儲(chǔ)同一元素,則應(yīng)使用此方法。
Syntax:
句法:
array_name = Array.new(size = 0, obj = nil)Parameter(s):
參數(shù):
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.
在這種方法中,參數(shù)起著非常重要的作用。 此方法將有兩個(gè)參數(shù),第一個(gè)是尺寸,第二個(gè)是元素。 如果不提供任何參數(shù),它將導(dǎo)致一個(gè)空數(shù)組,其中沒(méi)有任何元素。
Example 1:
范例1:
=beginRuby 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: 0Explanation:
說(shuō)明:
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)方法的實(shí)現(xiàn) 。 此方法為大小時(shí)間創(chuàng)建相同元素的副本。 您還可以觀察到,如果我們沒(méi)有為該方法提供參數(shù),則Array會(huì)生成一個(gè)空Array,并且借助Array.count method可以證明這一點(diǎn)。
Example 2:
范例2:
=beginRuby 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 arrOutput
輸出量
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 IncludeHelpRUN 2: Enter the element you want to keep in the Array instance 100 Enter the size of Array 3 Array elements are: 100 100 100Explanation:
說(shuō)明:
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.
在上面的代碼中,您可以觀察到我們從用戶(hù)那里獲取了兩個(gè)輸入,第一個(gè)是Array的大小,第二個(gè)是我們要存儲(chǔ)在其中的元素。 從輸出中可以看到,我們的Array實(shí)例包含該元素的大小編號(hào)。
翻譯自: https://www.includehelp.com/ruby/creating-array-with-array-new-size-obj.aspx
obj.val 非數(shù)組
總結(jié)
以上是生活随笔為你收集整理的obj.val 非数组_在Ruby中使用Array.new(size,obj)创建数组的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Scala中的嵌套循环
- 下一篇: java enummap_Java En