ruby array_在Ruby中使用Array.at()方法访问Array的元素

ruby array

We are very well known with the way to declare an Array in Ruby or you can say that how we can create an instance of Array class? We also know that Ruby has got a wealthy library where you can find various predefined methods and use them as per the requirement of your code. In this tutorial, we will get to learn about one method of Ruby library which is namely Array.at() method. This method is used for accessing or fetching a particular element from the object of the Array class.

我们在Ruby中声明数组的方式众所周知,或者可以说我们如何创建Array类的实例? 我们也知道Ruby有一个丰富的库,您可以在其中找到各种预定义的方法,并根据您的代码要求使用它们。 在本教程中,我们将学习Ruby库的一种方法,即Array.at()方法 。 此方法用于从Array类的对象访问或获取特定元素。

With the help of the Array.at() method, you can find any element at a particular index. This method is very helpful in most of the circumstances. In case you want to traverse an array or you can say that you want to print every element of the Array then you can process in the following way:

借助Array.at()方法 ,您可以找到特定索引处的任何元素。 在大多数情况下,此方法非常有用。 如果要遍历数组,或者可以说要打印数组的每个元素,则可以按以下方式处理:

    Array.at(index)

The above syntax will be more clear to you when you will implement it in code. Let us see how we can implement it in code?

当您在代码中实现上述语法时,对您来说将更加清晰。 让我们看看如何在代码中实现它?

Example 1:

范例1:

# array declaration
Adc = ['Includehelp.com','Ruby','c++','C#']

# input the index/position
puts "Enter the index of element you want to find"
inx = gets.chomp.to_i

# accessing the element and printing
puts "Element at index #{inx} is #{Adc.at(inx)}"

Output

输出量

RUN 1:
Enter the index of element you want to find
1
Element at index 1 is Ruby

RUN 2:
Enter the index of element you want to find
11
Element at index 11 is

Explanation:

说明:

You can find in the above code that we are trying to print the element in the Array which is present at a particular index entered by the user. In the Run 1, you can see that when the user has entered the proper index then the value is obtained but in the Run 2, when the user has entered the wrong index there is no output which simply means that this method will not give you error when you are entering an index which is out of the bound.

您可以在上面的代码中找到我们正在尝试打印Array中存在于用户输入的特定索引处的元素的元素。 在运行1中,您可以看到,当用户输入正确的索引时,便会获得该值;但是在运行2中,当用户输入了错误的索引时,则没有输出,这仅意味着该方法不会为您提供输入超出范围的索引时出错。

Example 2:

范例2:

# array declaration
Adc = ['Includehelp.com','Ruby','c++','C#','java','python']

# limit value 
puts "Enter the limit of traverse"
lm = gets.chomp.to_i

# loop to print index with element
i = 0
for i in 0..lm
  puts "Element at index #{i} is #{Adc.at(i)}"
end

Output

输出量

RUN 1:
Enter the index of element you want to find
5
Element at index 0 is Includehelp.com
Element at index 1 is Ruby
Element at index 2 is c++
Element at index 3 is c#
Element at index 4 is java
Element at index 5 is python

RUN 2:
Enter the index of element you want to find
8
Element at index 0 is Includehelp.com
Element at index 1 is Ruby
Element at index 2 is c++
Element at index 3 is c#
Element at index 4 is java
Element at index 5 is python
Element at index 6 is
Element at index 7 is
Element at index 8 is

Explanation:

说明:

In the above code, you can observe the Array.at() method can also be used to traverse the Array. Here, we are taking a limit from the user in the form of entered value from the console and traversing the Array as per the demand of the user. You can also observe that the program is not giving any kind of Exception when the limit is exceeding the total number of elements present in the Array.

在上面的代码中,您可以观察到Array.at()方法也可以用于遍历Array。 在这里,我们以从控制台输入的值的形式对用户进行限制,并根据用户的需求遍历数组。 您还可以观察到,当限制超出Array中存在的元素总数时,程序没有给出任何异常。

翻译自: https://www.includehelp.com/ruby/accessing-elements-of-array-with-array-at-method.aspx

ruby array

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值