ruby一维数组转二维数组_在Ruby中创建二维数组

本文介绍了在Ruby中声明和实现二维数组的两种方法:1) 使用方括号块声明,2) 利用Array.new方法。这两种方法帮助我们创建和操作二维数组,即使Ruby没有专门的二维数组类。
摘要由CSDN通过智能技术生成

ruby一维数组转二维数组

Ruby二维数组 (Ruby Two Dimensional Array)

So far we have discussed single-dimensional Array instances or 1D Array instances in Ruby. We have seen how we can declare them and how we can implement Array class methods on them. Ruby provides you many methods through which you can manipulate or shorten your code. In this article, we will see how we can declare and implement 2-D Array objects or the two-dimensional Arrays in Ruby? There is nothing like Two Dimensional Array class in Ruby or you can say that there is no separate class for double–dimension Arrays because two-dimensional arrays are nothing but the combination of two 1D Arrays. In this article, you will go through two ways through which you can declare two-dimensional arrays in Ruby.

到目前为止,我们已经讨论了Ruby中的一维数组实例或一维数组实例 。 我们已经看到了如何声明它们以及如何在它们上实现Array类方法。 Ruby为您提供了许多方法,您可以通过这些方法来操纵或缩短代码。 在本文中,我们将看到如何在Ruby中声明和实现二维数组对象或二维数组? Ruby中没有像二维数组类那样的东西,或者您可以说二维数组没有单独的类,因为二维数组不过是两个一维数组的组合。 在本文中,您将通过两种方式在Ruby中声明二维数组

The first way is a conventional way and the second way is away with some twist and they both are given below,

第一种方法是常规方法,第二种方法有些许扭曲,它们都在下面给出,

1)借助[]块进行声明 (1) Declaring with the help of [] block)

This is the very easiest and conventional method of declaring a 2D array. You only have to assign the values to the Array instance with the help of Assignment operator and square braces. The syntax and demonstrating example is given below,

这是声明2D数组的最简单和常规的方法。 您只需借助赋值运算符和方括号将值分配给Array实例。 语法和演示示例如下所示:

    array_name = [ [val1,val2], [val3,val4], ..., [valm,valn] ]

Example:

例:

=begin
    Ruby program to create 2 D Array
=end

# a two-dimensional array declaration
arr = [[1,2],[2,3],['Satish','MCA'],['Hrithik','BCA']]

# printing
puts "The two dimensional Array elements are:"
print arr

Output

输出量

The two dimensional Array elements are:
[[1, 2], [2, 3], ["Satish", "MCA"], ["Hrithik", "BCA"]]

Explanation:

说明:

In the above code, you can observe that we are creating a two-dimensional array most conveniently. We only have to assign values to the array_name and we are done with creating a two-dimensional array as we have done in the program code.

在上面的代码中,您可以看到我们最方便地创建了一个二维数组。 我们只需要给array_name赋值 ,就可以像在程序代码中那样创建二维数组了。

2)借助Array.new方法 (2) With the help of Array.new method)

We can create a two-dimensional array with the help of Array.new method as well. Only we have to pass the Array.new method as one of the arguments of the outer Array.new() method.

我们也可以借助Array.new方法 创建一个二维数组 。 只有我们必须将Array.new方法作为外部Array.new()方法的参数之一传递

We will understand the scenario in a better way when you will understand it with the help of Syntax and examples.

在语法和示例的帮助下,我们将以更好的方式理解该场景。

Syntax:

句法:

    array_name = Array.new(size, Array.new)

Example:

例:

=begin
    Ruby program to create 2 D Array
=end

# array declaration
arr = Array.new(2,Array.new)

# assigning values
arr[0][0] = "Hrithik"
arr[0][1] = "Includehelp"
arr[1][0] = "Hrithik"
arr[1][1] = "Includehelp"

# printing
print "Array elements are...\n"
print arr

Output

输出量

Array elements are...
[["Hrithik", "Includehelp"], ["Hrithik", "Includehelp"]]

Explanation:

说明:

In the above code, you can observe that you can create the Array in the above way as well. Later on, you can assign values in the above ways as well.

在上面的代码中,您可以观察到也可以通过上述方式创建Array。 稍后,您也可以通过上述方式分配值。

翻译自: https://www.includehelp.com/ruby/creating-two-dimensional-arrays.aspx

ruby一维数组转二维数组

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值