博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
前序遍历m-ary树_在Ruby中使用ary [start,length]-> object进行数组元素分配
阅读量:2532 次
发布时间:2019-05-11

本文共 4249 字,大约阅读时间需要 14 分钟。

前序遍历m-ary树

In the last article, we have learnt how we can add an object as an element to the object of Array class and we did that with the help of ? That was also one of the ways to assign elements to the Array instances because with the help of that method we were adding the element or object in the particular index of the Array instance and that index may be negative or positive. In this article, we will about one more way through which we can assign elements to the various indexes of the Array instance. At the end of the article, you will be aware of the method very clearly. Let us see the syntax and implementation of the method with the help of examples.

在上一篇文章中,我们学习了如何将对象作为元素添加到Array类的对象中,并借助 ? 这也是将元素分配给Array实例的方法之一,因为在该方法的帮助下,我们将元素或对象添加到Array实例的特定索引中,并且该索引可能是负数或正数。 在本文中,我们将介绍另一种方法,可以通过该方法将元素分配给Array实例的各个索引。 在本文的结尾,您将非常清楚地了解该方法。 让我们借助示例来了解该方法的语法和实现。

用ary [start,length]分配元素 (Assigning element with ary[start,length])

In this way of assignment, you will have to provide two indexes of the Array instance where you want to put your element or object of a particular class. You will have to provide two indexes means that one index will refer to the starting index and the second index will refer to the closing index. Got confused? Let me clear this with the help of a theoretical example if you are providing 1 and 3 as the index then the elements will be assigned at index 1,2 and 3. Now, let us understand the scenario or method with the help of syntax and examples which are provided below,

通过这种分配方式,您将必须提供Array实例的两个索引,您要在其中放置特定类的元素或对象。 您将必须提供两个索引,这意味着一个索引将引用起始索引,第二个索引将引用结束索引。 感到困惑? 让我借助一个理论示例来清除此问题,如果您提供1和3作为索引,那么将在索引1,2和3处分配元素。现在,让我们借助语法和方法了解场景或方法。下面提供的示例

Syntax:

句法:

array_instance[start,end] = object or object(s)

Example 1:

范例1:

=begin  Ruby program to demonstrate   Array_instance[start,end] = object=end# array declarationarray_instance = ["a","c","c","v","samir","Hrithik"]# input the indexesputs "Enter the start index you want to put element in:"start = gets.chomp.to_iputs "Enter the end index you want to put element in:"endi = gets.chomp.to_iif(endi
0)  for i in start..endi puts "Enter the element:" array_instance[i] = gets.chomp endelse puts "Index out of bound"end# printing the array puts "The final array is:"print array_instance

Output

输出量

Enter the start index you want to put element in: 1Enter the end index you want to put element in: 3Enter the element: HrithikEnter the element: ShivangEnter the element: PremThe final array is:["a", "Hrithik", "Shivang", "Prem", "samir", "Hrithik"]

Explanation:

说明:

In the above code, you can observe that we are asking the user for two indexes. If both the indexes can fulfill the "if" condition of our code, then further processing is taking place. With the help of "for" loop and range, we are asking the user for the elements. You can observe that our elements are reflected in the final Array after overwriting the previous elements assigned at the time of declaring Array instance.

在上面的代码中,您可以观察到我们在询问用户两个索引。 如果两个索引都可以满足我们代码的“ if”条件,那么将进行进一步处理。 借助“ for”循环和范围,我们正在向用户询问元素。 在覆盖声明Array实例时分配的先前元素之后,您可以观察到我们的元素反映在最终Array中。

Example 2:

范例2:

=begin  Ruby program to demonstrate   Array_instance[start,end] = object=end# array declarationarray_instance = ["a","c","c","v","samir","Hrithik"]# input the indexesputs "Enter the start index you want to put element in:"start = gets.chomp.to_iputs "Enter the end index you want to put element in:"endi = gets.chomp.to_iputs "Enter the element:"# array instancearray_instance[start,endi] = gets.chomp# printing the arrayputs "The final array is:"print array_instance

Output

输出量

Enter the start index you want to put element in: 1Enter the end index you want to put element in: 3Enter the element: ?The final array is:["a", "?", "samir", "Hrithik"]

Explanation:

说明:

In the above code, you can observe that we are taking input from the user for two indexes and we are demanding only a single element. This example is introduced just to show you that when you provide a single element to the range, it specifies the whole range a single element that can be accessed by the start index only.

在上面的代码中,您可以观察到我们从用户那里获取了两个索引的输入,而我们只需要一个元素。 引入此示例只是为了向您展示,当您向范围提供单个元素时,它指定了整个范围内的单个元素,该元素只能由起始索引访问。

翻译自:

前序遍历m-ary树

转载地址:http://sbazd.baihongyu.com/

你可能感兴趣的文章
Http协议的学习
查看>>
【转】轻松记住大端小端的含义(附对大端和小端的解释)
查看>>
设计模式那点事读书笔记(3)----建造者模式
查看>>
ActiveMQ学习笔记(1)----初识ActiveMQ
查看>>
Java与算法之(2) - 快速排序
查看>>
Windows之IOCP
查看>>
机器学习降维之主成分分析
查看>>
CTP2交易所成交回报
查看>>
WebSocket & websockets
查看>>
openssl 升级
查看>>
ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
查看>>
CVE-2010-2883Adobe Reader和Acrobat CoolType.dll栈缓冲区溢出漏洞分析
查看>>
使用正确的姿势跨域
查看>>
AccountManager教程
查看>>
Android学习笔记(十一)——从意图返回结果
查看>>
算法导论笔记(四)算法分析常用符号
查看>>
ultraedit激活
查看>>
总结(6)--- python基础知识点小结(细全)
查看>>
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>