CSE 341 - Smalltalk vs. Java syntax

Reload this page often! Send me a snippet Smalltalk, and I'll try my best to translate it to Java.

iterating over an array

From Notes on Smalltalk, Example 1. Note that object 1 is being sent a keyword message to: {limit} do: {block}, where the block is parameterized to accept a loop counter value. Both reading and writing Array objects are illustrated with the keyword messages at: {source index} and at: {destination index} put: {value to store there}. Everything is done with messages! No special syntax for array indexing, as in Java, C, and C++. It's also worth noting that in Java, array indexing is 0-based, so the code given on the right wouldn't quite work. (requested by Ting Yip)

1 to: oldStore size do:
  [:k | store at: k put: (oldStore at: k)]].
for (int k = 1; i <= oldStore.length; i++) {
  store[k] = oldStore[k];
}