- Why does the following code not do the job?
- How does ‘a’ get changed after Step 2?
-
What is the best solution to accomplish the task?
// 1. Create a 2D array 'a' val a = Array.ofDim[String](2, 2) a(0)(0) = "TL" a(0)(1) = "TR" a(1)(0) = "BL" a(1)(1) = "BR" // 2. Swap two elements of 'a' to create a new 2D array 'b' val b = a b(0)(0) = a(0)(1) b(0)(1) = a(0)(0) // Output println("a:") println(a(0)(0) + " " + a(0)(1)) println(a(1)(0) + " " + a(1)(1)) println("b:") println(b(0)(0) + " " + b(0)(1)) println(b(1)(0) + " " + b(1)(1)) Result: a: TR **TR** BL BR b: TR **TR** BL BR
Why does the following code not do the job? How does ‘a’ get changed
Share
Copy your 2D array like this to create a deep clone: