如何在Swift中编写一个2D字符数组?

时间:2021-10-17 20:58:00

I'm trying to translate this Java code into Swift, but I'm having no luck so far.

我试着把这个Java代码转换成Swift代码,但是到目前为止我没有什么运气。

public final int rows = 6;
public final int columns = 7;
public char board[][] = new char[rows][columns];

Basically, I want to create a 2D array of (Character)s with the rows and columns variable giving the dimensions (size) of the 2D array.

基本上,我希望创建一个2D数组(字符),其中行和列变量赋予2D数组的尺寸(大小)。

2 个解决方案

#1


0  

var rows: Int = 6 var columns: Int = 7

var行:Int = 6 var列:Int = 7。

let data: [[Character]] = [[Character]](count: rows, repeatedValue: [Character](count: columns, repeatedValue: " "))

让数据:[[字符]]=[[字符]](count: rows, repeatedValue: [Character](count: columns, repeatedValue: "))

It will initialise all the Characters with empty values

它将初始化所有具有空值的字符。

#2


0  

var rows: Int = 6
var columns: Int = 7
var boardColumns: [Character] = [Character](count: rows, repeatedValue: "c")
var board: [[Character]] = [[Character]](count: columns, repeatedValue: boardColumns)

In swift you have to initialize a non optional variable at declaration. You could make this an optional by doing like so:

在swift中,您必须在声明中初始化一个非可选变量。你可以这样做可选:

var rows: Int = 6
var columns: Int = 7
var boardColumns: [Character?] = [Character?](count: rows, repeatedValue: nil)
var board: [[Character?]] = [[Character?]](count: columns, repeatedValue: boardColumns)

to make the values in the array optional, or you could declare the whole thing as an optional, but you have to declare it as something before you can use it.

为了使数组中的值是可选的,或者您可以将整个事件声明为可选的,但是您必须在您可以使用它之前声明它。

var board: [[Character]]?

If you want to append values on to it, you can to initialize it like so

如果您想要将值追加到它,您可以像这样初始化它。

var board: [[Character]] = [[]]

#1


0  

var rows: Int = 6 var columns: Int = 7

var行:Int = 6 var列:Int = 7。

let data: [[Character]] = [[Character]](count: rows, repeatedValue: [Character](count: columns, repeatedValue: " "))

让数据:[[字符]]=[[字符]](count: rows, repeatedValue: [Character](count: columns, repeatedValue: "))

It will initialise all the Characters with empty values

它将初始化所有具有空值的字符。

#2


0  

var rows: Int = 6
var columns: Int = 7
var boardColumns: [Character] = [Character](count: rows, repeatedValue: "c")
var board: [[Character]] = [[Character]](count: columns, repeatedValue: boardColumns)

In swift you have to initialize a non optional variable at declaration. You could make this an optional by doing like so:

在swift中,您必须在声明中初始化一个非可选变量。你可以这样做可选:

var rows: Int = 6
var columns: Int = 7
var boardColumns: [Character?] = [Character?](count: rows, repeatedValue: nil)
var board: [[Character?]] = [[Character?]](count: columns, repeatedValue: boardColumns)

to make the values in the array optional, or you could declare the whole thing as an optional, but you have to declare it as something before you can use it.

为了使数组中的值是可选的,或者您可以将整个事件声明为可选的,但是您必须在您可以使用它之前声明它。

var board: [[Character]]?

If you want to append values on to it, you can to initialize it like so

如果您想要将值追加到它,您可以像这样初始化它。

var board: [[Character]] = [[]]