多次元配列を複製する場合の結果は1次元配列を複製する場合と同様で、長さ・次元数・格納される要素が同一の多次元配列が生成されます。
Array.Cloneメソッドで2次元配列の複製を作成する
Imports System
Class Sample
Shared Sub Main()
Dim matrix1 As Integer(,) = { _
{0, 1, 2, 3}, _
{4, 5, 6, 7}, _
{8, 9, 10, 11} _
}
Dim matrix2 As Integer(,)
' matrix1を複製してmatrix2に代入
matrix2 = DirectCast(matrix1.Clone(), Integer(,))
' 複製元と複製後の配列の要素を変更
matrix1(0, 0) = 99
matrix2(0, 0) = -1
' それぞれの配列の内容を表示
For d1 As Integer = 0 To matrix1.GetLength(0) - 1
For d2 As Integer = 0 To matrix1.GetLength(1) - 1
Console.Write("{0}, ", matrix1(d1, d2))
Next
Console.WriteLine()
Next
Console.WriteLine()
For d1 As Integer = 0 To matrix2.GetLength(0) - 1
For d2 As Integer = 0 To matrix2.GetLength(1) - 1
Console.Write("{0}, ", matrix2(d1, d2))
Next
Console.WriteLine()
Next
End Sub
End Class
実行結果
99, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,