Recursive method in pharo produces #SubscriptOutOfBounds:8

北战南征 提交于 2020-01-06 06:02:51

问题


I created a class in Pharo known as BinarySearchTreean i implemented a method called BinarySearchTree>>PreOrder and BinarySearchTree>>index

Preorder: myArray index: position

(myArray at: position) ~= -1 
ifTrue: [ 
       Transcript show: (myArray at: position).
        self Preorder: myArray index: (position * 2).
        self Preorder: myArray index: (position * 2) + 1.
    ].

I then provided this array #(90 60 95 50) with index 1 to make a PreOrder search in my binary tree which i implemented using arrays but it does not work. Please help...


回答1:


#at: will signal SubscriptOutOfBounds when the index is < 0 or greater than the size of the array (Smalltalk collections are 1-based, i.e. the first index is 1, not 0). 8 is clearly larger than 4 (the size of myArray).

The check at the start will never evaluate to False as your array has no entry -1, and hence the conditional block will be evaluated every time.

I can't really say where your problem lies as you've excluded all the code that is actually of interest. If you add that I can tell you more.



来源:https://stackoverflow.com/questions/46619649/recursive-method-in-pharo-produces-subscriptoutofbounds8

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!