前序遍历m-ary树_在Ruby中使用ary [start,length]- object进行数组元素分配
前序遍歷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 Array_instance[index] operator? 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_instance [index]運算符實現了這一點 ? 這也是將元素分配給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:
=beginRuby program to demonstrate Array_instance[start,end] = object =end# array declaration array_instance = ["a","c","c","v","samir","Hrithik"]# input the indexes puts "Enter the start index you want to put element in:" start = gets.chomp.to_i puts "Enter the end index you want to put element in:" endi = gets.chomp.to_iif(endi<array_instance.count && start>0)for i in start..endiputs "Enter the element:"array_instance[i] = gets.chompend elseputs "Index out of bound" end# printing the array puts "The final array is:" print array_instanceOutput
輸出量
Enter the start index you want to put element in:1 Enter the end index you want to put element in:3 Enter the element:Hrithik Enter the element:Shivang Enter the element:Prem The 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:
=beginRuby program to demonstrate Array_instance[start,end] = object =end# array declaration array_instance = ["a","c","c","v","samir","Hrithik"]# input the indexes puts "Enter the start index you want to put element in:" start = gets.chomp.to_i puts "Enter the end index you want to put element in:" endi = gets.chomp.to_i puts "Enter the element:"# array instance array_instance[start,endi] = gets.chomp# printing the array puts "The final array is:" print array_instanceOutput
輸出量
Enter the start index you want to put element in:1 Enter the end index you want to put element in:3 Enter 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.
在上面的代碼中,您可以觀察到我們從用戶那里獲取了兩個索引的輸入,而我們只需要一個元素。 引入此示例只是為了向您展示,當您向范圍提供單個元素時,它指定了整個范圍內的單個元素,該元素只能由起始索引訪問。
翻譯自: https://www.includehelp.com/ruby/array-element-assignment-with-ary-start-length-object.aspx
前序遍歷m-ary樹
總結
以上是生活随笔為你收集整理的前序遍历m-ary树_在Ruby中使用ary [start,length]- object进行数组元素分配的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 创维酷开电视的质量怎么样?
- 下一篇: 合页损失,铰链损失_多点铰链损失功能 使