Implement Matrix whith OrderedCollection

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Implement Matrix whith OrderedCollection

frankl1_miky
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?
Reply | Threaded
Open this post in threaded view
|

Re: Implement Matrix whith OrderedCollection

Sven Van Caekenberghe-2
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.
>


Reply | Threaded
Open this post in threaded view
|

Re: Implement Matrix whith OrderedCollection

frankl1_miky
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.

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.
>




If you reply to this email, your message will be added to the discussion below:
http://forum.world.st/Implement-Matrix-whith-OrderedCollection-tp4945002p4945003.html
To unsubscribe from Implement Matrix whith OrderedCollection, click here.
NAML