ruby数组长度统计_在Ruby中到数组长度的不同⽅法
ruby 数组长度统计
If you are reading an article that is related to obtaining information about the instance of Array class then it is expected from you that you are aware of the basic things related to Array, such as how to declare it?
如果您正在阅读与获取有关Array类的实例信息有关的⽂章,那么您应该了解与Array有关的基本知识,例如如何声明它?
In this article, you will come to know about various methods that are available in Ruby's library specifically defined
various methods that are available in Ruby's library specifically defined to obtain the length of the Array. We are very well aware of the fact that are dynamic which means that we can store n to obtain the length of the Array
number of elements in them without taking care or bothering about their predefined size. So, you will need to check the number of elements present in the Array at a particular point of time while working with . We have got multiple methods
Array.length unt
which are defined in Ruby's library to serve this purpose, they are unt
在Ruby的库中可⽤的各种⽅法,这些⽅法是专门为获取Array的长度⽽定义的 。 我们⾮常清楚是动态的,这意在本⽂中,您将了解在Ruby的库中可⽤的各种⽅法,这些⽅法是专门为获取Array的长度⽽定义的
味着我们可以在其中存储n个元素,⽽不必担⼼或打扰它们的预定义⼤⼩。 因此,在使⽤时,您需要在特定时间检查Array中存在的元素
Array.length和unt
数。 我们在Ruby的库中定义了多种⽅法来实现此⽬的,它们分别是unt
In the rest of the article, you will get to see the four ways through which you can obtain information about the number of elements present in the object of Array class.
在本⽂的其余部分,您将了解四种获取有关Array类对象中存在的元素数量的信息的⽅式。
1)Array.each⽅法 (1) Array.each method)
Syntax:
句法:
Array.each do |element|
end
Program:
程序:
=begin
Ruby program to find length using Array.each method
=end
python获取数组长度
# array declaration
Adc = ['Includehelp','Ruby','C++','C#','Java','Python']
# counting length
cont = 0
Adc.each do |ele|
cont=cont+1
end
# printing the lenght
puts "Number of elements present in Array are #{cont}"
Output
输出量
Number of elements present in Array are 6
Explanation:
说明:
Array.each In the above code, you must have observed that we have declared a variable named cont and using it inside the Array.each method. Every time, when the method is finding any element in the Array, the value of cont is increased by one.
method
Array.each⽅法中使⽤了它。 每当⽅法在数组中到任何元在上⾯的代码中,您必须观察到我们已经声明了⼀个名为cont的变量,并在Array.each⽅法中
素时, cont的值将增加⼀。
unt⽅法 (2) unt method)
syntax and code given below.
我们也可以借助unt⽅法
Syntax:
句法:
Program:
程序:
=begin
Ruby program to find length unt method
=end
# array declaration
Adc = ['Includehelp','Ruby','C++','C#','Java','Python']
# counting array length
num = unt
# printing the length
puts "Number of elements present in Array are #{num}"
Output
输出量
Number of elements present in Array are 6
3)Array.length⽅法 (3) Array.length method)
Array.length method as well. Refer to the We can find the number of elements present in the Array with the help of the Array.length method
syntax and code given below.
Array.length⽅法到Array中存在的元素数量。 请参阅下⾯给出的语法和代码。
我们也可以借助Array.length⽅法
Syntax:
句法:
Array.length
Program:
程序:
=begin
Ruby program to find length using Array.length method
=end
# array declaration
Adc = ['Includehelp','Ruby','C++','C#','Java','Python']
# counting the array length
num = Adc.length
# printing the array length
puts "Number of elements present in Array are #{num}"
Output
输出量
Number of elements present in Array are 6
4)Array.size⽅法 (4) Array.size method)
Array.size method as well. Refer to the We can find the number of elements present in the Array with the help of the Array.size method
syntax and code given below.
我们也可以借助Array.size⽅法
Array.size⽅法到Array中存在的元素数量。 请参阅下⾯给出的语法和代码。
Syntax:
句法:
Array.size
Program:
程序:
=begin
Ruby program to find length using Array.size method
=end
# array declaration
Adc = ['Includehelp','Ruby','C++','C#','Java','Python']
# counting array length
num = Adc.size
# printing the array length
puts "Number of elements present in Array are #{num}"
Output
输出量
Number of elements present in Array are 6
ruby 数组长度统计

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。