[英]How to Create 2D arrays in smalltalk
我已經按照這個previuos線程如何在Smalltalk中管理2d數組? 但它沒有幫助我請幫幫我。
我正在嘗試創建NXN陣列,然后打印出來。 對於exxple 2x2陣列:我缺少什么?
|testArr|.
testArr := (Array new: 2)
at: 1 put: ((Array new: 2) at: 1 put: '0'; at: 2 put: 'X');
at: 2 put: ((Array new: 2) at: 1 put: 'P'; at: 2 put: 'Y').
1 to:2 do:[:a|
1 to:2 do:[:b|
Transcript show: testArr at:a at:b.
].
].
錯誤是在抄本與未知選擇器。 我該怎么辦才能解決這個問題?
您的代碼有幾個問題:
首先是您描述的錯誤消息。 我想全長說:
MessageNotUnderstood:ThreadSafeTranscript >> show:at:at:
所以這意味着您應該設置一些括號以將正確的消息發送到正確的對象。 嘗試:
Transcript show: ((testArr at:a) at:b).
現在,您的陣列分配也存在問題。
在Smalltalk / Pharo / Squeak中,如果你發送at:put:
到一個數組,它返回你指定的對象,第二個參數at:put:
而不是接收者。 因此在您的示例中,變量testArr
不包含數組數組,但字符串為“Y”。
如果你真的想使用消息級聯;
,您必須在級聯結束時將yourself
消息發送到數組。
像這樣:
testArr := (Array new: 2).
testArr at: 1 put: ((Array new: 2) at: 1 put: '0'; at: 2 put: 'X'; yourself).
testArr at: 2 put: ((Array new: 2) at: 1 put: 'P'; at: 2 put: 'Y'; yourself).
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.