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 非數組