This post was updated on .
Hi everyone,
I'm figuring out a strange behavoir with OrderedCollection. Actually I've created a matrix using OrderedCollection like this: c := OrderedCollection new: 16 withAll: (OrderedCollection new: 16 withAll: 0). But when I try to set the value at row 1 and column 2 to be 50 like this: (c at: 1) at: 2 put: 50 I instead get 50 in any cell of the second column and I don't know why. When using a Matrix and the massage at:at:put, the behavoir is normal. Why that strange behavoir with OrderedCollection? |
Because all 16 elements of the first/top ordered collection contain the exact same element. They are all #==, the same instance/object. If you change one, you change them all.
Here is one way to create what I think you want: OrderedCollection new: 16 streamContents: [ :out | 16 timesRepeat: [ out nextPut: (OrderedCollection new: 16 withAll: 0) ] ] BTW, Arrays would be better in this case, I think. > On 30 Apr 2017, at 23:08, frankl1_miky <[hidden email]> wrote: > > Hi everyone, > > I'm figuring out a strange behavoir with OrderedCollection. Actually I've > created a matrix using OrderedCollection like this: > > /c := OrderedCollection new: 16 withAll: (OrderedCollection new: 16 withAll: > 0). > c./ > But when I try to set the value at row 1 and column 2 to be 50 like this: > /(c at: 1) at: 2 put: 50/ > I instead get 50 in any cell of the second column and I don't know why. > > When using a Matrix and the massage at:at:put, the behavoir is normal. > > Why that strange behavoir with OrderedCollection? > > > > -- > View this message in context: http://forum.world.st/Implement-Matrix-whith-OrderedCollection-tp4945002.html > Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com. > |
Tanks, that answer absolutely explain everting Le 30 avr. 2017 10:29 PM, "Sven Van Caekenberghe-2 [via Smalltalk]" <[hidden email]> a écrit : Because all 16 elements of the first/top ordered collection contain the exact same element. They are all #==, the same instance/object. If you change one, you change them all. |
Free forum by Nabble | Edit this page |