博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
obj.val 非数组_在Ruby中使用Array.new(size,obj)创建数组
阅读量:2529 次
发布时间:2019-05-11

本文共 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/

你可能感兴趣的文章
java学习笔记④MySql数据库--01/02 database table 数据的增删改
查看>>
两台电脑如何实现共享文件
查看>>
组合模式Composite
查看>>
程序员最想得到的十大证件,你最想得到哪个?
查看>>
我的第一篇CBBLOGS博客
查看>>
【MyBean调试笔记】接口的使用和清理
查看>>
07 js自定义函数
查看>>
jQueru中数据交换格式XML和JSON对比
查看>>
form表单序列化后的数据转json对象
查看>>
[PYTHON]一个简单的单元測试框架
查看>>
iOS开发网络篇—XML数据的解析
查看>>
[BZOJ4303]数列
查看>>
一般处理程序在VS2012中打开问题
查看>>
C语言中的++和--
查看>>
thinkphp3.2.3入口文件详解
查看>>
POJ 1141 Brackets Sequence
查看>>
Ubuntu 18.04 root 使用ssh密钥远程登陆
查看>>
Servlet和JSP的异同。
查看>>
虚拟机centOs Linux与Windows之间的文件传输
查看>>
ethereum(以太坊)(二)--合约中属性和行为的访问权限
查看>>