前序遍历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:
=begin
Ruby 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
if(endi<array_instance.count && start>0)
for i in start..endi
puts "Enter the element:"
array_instance[i] = gets.chomp
end
else
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:
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:
=begin
Ruby 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_instance
Output
输出量
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树