简体   繁体   English

如何快速创建 3D 数组/矩阵?

[英]How to create a 3D array/matrix in swift?

I am trying to create a 3D array which will store an image's height, width, and RGB values as the dimensions of matrix/array.我正在尝试创建一个 3D 数组,它将存储图像的高度、宽度和 RGB 值作为矩阵/数组的维度。 Here is what I am trying:这是我正在尝试的:

var array = Array<Array<Array<Int>>>()
var testArray = [[[Int]]] ()

Now that I have initialize these arrays, how would I set a specific number of rows and columns?现在我已经初始化了这些数组,我将如何设置特定数量的行和列? Thanks!谢谢!

Until fixed length arrays are supported in Swift, I'd do it something like this:在 Swift 支持固定长度数组之前,我会这样做:

let zArry = [Int](repeating: 0, count: 10)
let yArry = [[Int]](repeating: zArry, count: 20)
let xArry = [[[Int]]](repeating: yArry, count: 30)

let val = xArry[2][1][1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM