When you instantiate an object in Scala, you can parameterize it with values and types.
The follwing Scala coe instantiates a new java.math.BigInteger and parameterizes it with the value "12345"
val big = new java.math.BigInteger("12345")
val greetString = new Array[String](3)
greetStrings(0) = "Hello"
greetStrings(1) = ", "
greetStrings(2) = "world!\n"
val greetStrings : Array[String] = new Array[String](3)
greetString(0) = "Hello"->
greetString(1) = ", "
greetString(2) = "world!\n"
These three lines of code illustrate an important concept to understand about Scala concerning the meaning of val. When you define a variable with val, the variable can't reassigned, but the object to which it refers could potentially still be changed. So in this case, you couldn't be reassign greetStrings to a different array; greetStrings will always point to the same Array[String] instance with which it was initialized. But you can changed the elements of that array[String] instance with which it was initialized. But you can change the elements of that Array[String] over time, so the array itself is mutable.
for( i <- 0="" 2="" p="" to=""> print(greetString(i))
The code 0 to 2 is transformed into the method call (0).to(2).
Note that this syntax only works if you explicitily specify the receiver of the method call. You cannot write "println 10", but you can write "Console println 10".->
greetStrings(0) = "Hello" => greetStrings.update(0,"Hello")
val greetStrings = new Array[String](3)
greetStrings.update(0,"hello")
greetStrings.update(1,", ")
greetStrings.update(2, "world!\n")
for(i <- 0.to="" p=""> print(greetStrings.apply(i))
->
댓글 없음:
댓글 쓰기