前序遍历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 >> operator? That was also one of the ways to assign elements to the Array instances because with the help of >> operator or method we were pushing or adding the element or object in the last index of the Array instance. 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.
在上一篇文章中,我们学习了如何将对象作为元素添加到Array类的对象中,并在>>运算符的帮助下做到了这一点? 这也是将元素分配给Array实例的方法之一,因为在>>运算符或方法的帮助下,我们将元素或对象推入或添加到Array实例的最后一个索引中。 在本文中,我们将介绍另一种方法,可以通过该方法将元素分配给Array实例的各个索引。 在本文的结尾,您将非常清楚地了解该方法。
Let us see the syntax and implementation of the method with the help of examples.
让我们借助示例来了解该方法的语法和实现。
用ary [index]分配元素 (Assigning element with ary[index])
In this way of assignment, you will have to provide the particular index of the Array instance where you want to put your element or object of a particular class. If you will provide a negative index, then it will start counting it from the end. Got confused? Let me explain the scenario with the help of theoretical example, if you are providing -1 as the index then it will assign the object to the first index of the Array instance from the last and if you pass -2 as the index, then it will assign the object to the second last index of the Array object. Now, let us understand the scenario or method with the help of syntax and examples which are provided below,
通过这种分配方式,您将必须提供Array实例的特定索引,您要在其中放置特定类的元素或对象。 如果您将提供一个负索引,那么它将从头开始计数。 感到困惑? 让我借助理论示例来解释这种情况,如果您提供-1作为索引,那么它将把对象分配给Array实例从最后一个开始的第一个索引,如果您传递-2作为索引,那么它将将对象分配给Array对象的倒数第二个索引。 现在,让我们借助下面提供的语法和示例来了解场景或方法,
Syntax:
句法:
array_instance[index] = object
Example 1:
范例1:
=begin
Ruby program to demonstrate
array_instance[index] = object
=end
# array declaration
array_instance = ["a","c","c","v","samir","Hrithik"]
# input the index and element
puts "Enter the index you want to put element in:"
ind = gets.chomp.to_i
if(ind<array_instance.count)
puts "Enter the element:"
array_instance[ind] = gets.chomp
else
puts "Index out of bound"
end
# printing the array
puts "The final array is:"
print array_instance
Output
输出量
Enter the index you want to put element in:
2
Enter the element:
Prem
The final array is:
["a", "c", "Prem", "v", "samir", "Hrithik"]
Explanation:
说明:
In the above code, you can observe that we are asking the index from the user in which he wants to put his element or object. First, we are checking whether the index is feasible if it is more than or equal to the length of Array, we are notifying the user. If this is not the case then we are putting the element in that particular index. From the above example, you can observe that an object is already present in that particular index but we have overwritten it with the help of this method. So, we can conclude that this method can be used for overwriting objects also.
在上面的代码中,您可以观察到我们正在询问用户要在其中放置元素或对象的索引。 首先,我们正在检查索引是否大于或等于Array的长度是否可行,然后通知用户。 如果不是这种情况,那么我们会将元素放入该特定索引中。 从上面的示例中,您可以观察到该特定索引中已经存在一个对象,但是我们借助此方法将其覆盖。 因此,我们可以得出结论,该方法也可以用于覆盖对象。
Example 2:
范例2:
=begin
Ruby program to demonstrate
array_instance[index] = object
=end
# array declaration
array_instance = ["a","c","c","v","samir","Hrithik"]
# input index and element
puts "Enter the negative index you want to put element in:"
ind = gets.chomp.to_i
puts "Enter the element:"
array_instance[ind] = gets.chomp
# printing the array
puts "The final array is:"
print array_instance
Output
输出量
Enter the negative index you want to put element in:
-2
Enter the element:
Prem
The final array is:
["a", "c", "c", "v", "Prem", "Hrithik"]
Explanation:
说明:
In the above code, you can observe that we are taking input from the user for the negative index. You can see that when the user has entered -2, then we have printed it and found that our object is residing in the second last element of the Array instance. The method has overwritten the index from "samir" to "Prem".
在上面的代码中,您可以观察到我们正在从用户那里获取负索引的输入。 您可以看到,当用户输入-2时,我们已经将其打印出来,发现我们的对象位于Array实例的倒数第二个元素中。 该方法已将索引从“ samir”覆盖为“ Prem”。
翻译自: https://www.includehelp.com/ruby/array-element-assignment-with-ary-index-object.aspx
前序遍历m-ary树