简体   繁体   English

初始化2D阵列

[英]Initializing a 2D array

I have the below Java code which I need to convert to Swift. 我有下面的Java代码,我需要将其转换为Swift。 First one is simply initializing a 2D array with a number of rows and columns. 第一个是简单地初始化具有许多行和列的2D数组。

double[][] VAR1 = new double[5][10];

Second one is initializing a 2D array with some initial values. 第二个方法是使用一些初始值初始化2D数组。

double[][] VAR2 = new double[][]{ {2.1, 4.3}, {5.4, 8.9},};

I can't figure out how to do the first one. 我不知道怎么做第一个。

But I have a shot at the second one. 但是我在第二个上有机会。 Not sure if it's correct though. 不确定是否正确。

var var2: [[Double]] = [[2.1], [4.3], [5.4], [8.9]]

You're close - should be like this: 您接近-应该是这样的:

var var2: [[Double]] = [[2.1, 4.3], [5.4, 8.9]]

And for the first one: 对于第一个:

var var1: [[Double]] = Array(repeating: Array(repeating: 0.0, count: 10), count: 5)

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

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