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 非数组

obj.val 非数组_在Ruby中使用Array.new(size,obj)创建数组相关推荐

  1. 用数组循环实现矩阵乘法php,array用法 numpy_从创建数组到矩阵运算,一文带你看懂Numpy...

    导读:Numpy(Numerical Python的简称)是高性能科学计算和数据分析的基础包,其提供了矩阵运算的功能.本文带你了解Numpy的一些核心知识点. 作者:魏溪含 涂铭 张修鹏 如需转载请联 ...

  2. ruby三元操作符_在Ruby中使用操作符将元素添加到数组实例中

    ruby三元操作符 In the previous articles, we have gone through ways through which we can create Array inst ...

  3. mysql编程的二维数组_调出mysql中数据,输出一个二维数组的表格

    1.使用DDL语句创建数据库.创建表. mysql> show databases; +--------------------+ | Database | +----------------- ...

  4. golang 包含 数组_在 Golang 中如何快速判断字符串是否在一个数组中

    在使用 Python 的时候,如果要判断一个字符串是否在另一个包含字符串的列表中,可以使用in 关键词,例如: name_list= ['pm', 'kingname', '青南'] if 'king ...

  5. ruby 集合 分组_在Ruby中打印集合的元素

    ruby 集合 分组 We have gone through the implementation of sets in Ruby. They are very similar to arrays. ...

  6. ruby 数组元素替换_从Ruby中的集合中删除并替换元素

    ruby 数组元素替换 Ruby has various specific methods to fulfil specific tasks. At several places, you may n ...

  7. ruby 数组自定义排序_在Ruby中对数组排序

    ruby 数组自定义排序 Sorting was a preoccupation for computer scientists from early on. There were many algo ...

  8. _.uniq_在Ruby中使用Array.compact和Array.uniq方法从Array中移除元素

    _.uniq Ruby Array.compact和Array.uniq方法 (Ruby Array.compact and Array.uniq Methods) In the last artic ...

  9. javascript字典中添加数组_在javascript中合并两个字典数组

    您可以使用 Array#map方法生成新数组(假设两个数组的顺序相同). var lat = [{key:"2017-09-20T11:51:32.000Z", value:50. ...

最新文章

  1. PHP大数组过滤元素、修改元素性能分析
  2. php跨域cookie共享使用方法
  3. Could not find a version that satisfies the requirement pyspider (from versions: ) No matching distr
  4. 敏捷开发思想及Scrum实践
  5. Containers vs Serverless:怎么选择?
  6. 银行不能成为外国资本的“***”
  7. 利用SQL模糊匹配来验证字段是否是日期格式
  8. cuda二维数组内存分配和数据拷贝
  9. redis java 监听_从零手写实现redis(四)添加监听器
  10. C++设计模式-桥接模式
  11. VSCode配置JAVA开发环境,java初级面试笔试题
  12. Activity过渡动画
  13. Hadoop大数据开发技术
  14. vue表格中的内容换行与导出Excel换行
  15. 使用python制作时间戳转换工具
  16. 网站被反诈中心DNS劫持解决教程
  17. 浏览器环境 兼容运行ES6语法
  18. 删除 Mac OS X 中“打开方式”里重复或无用的程序列表
  19. 《黄帝内经》的理论体系
  20. windows环境下elasticsearch使用教程

热门文章

  1. c语言计算机编程例题详解,计算机C语言编写程序题及答案解析精选.doc
  2. php times33,PHP Hash算法:Times33算法代码实例
  3. 微信小程序setinterval_简单谈谈setTimeout与setInterval
  4. python 除数不能为零的报错有哪些_【社区精选40】Python错误处理及代码调试方法(文末赠书中奖名单)...
  5. corssover linux运行无效,使用 CrossOver 在 Linux运行 Windows 软件(金测OK)
  6. 数值分析方程求根实验matlab,数值分析实验之非线性方程求根(MATLAB实现)
  7. java将date类型转成yyyymmdd_java中的Date怎么转换成YYYYMMDD形式?
  8. xtrabackup备份脚本
  9. c#输出最大值、最小值和平均值(B)【C#】
  10. AMD推出7nm高端显卡Radeon VII,直指英伟达RTX 2080