Swift - Arrays: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „== Multidimensionale Arrays == https://stackoverflow.com/questions/25127700/two-dimensional-array-in-swift Define mutable array <syntaxhighlight lang="swift…“)
 
Zeile 10: Zeile 10:
  
 
OR:
 
OR:
 
+
<syntaxhighlight lang="swift">
 
// 2 dimensional array of arrays of Ints  
 
// 2 dimensional array of arrays of Ints  
 
var arr: [[Int]] = []  
 
var arr: [[Int]] = []  
 +
</syntaxhighlight>
  
 
OR if you need an array of predefined size (as mentioned by @0x7fffffff in comments):
 
OR if you need an array of predefined size (as mentioned by @0x7fffffff in comments):
  
 +
<syntaxhighlight lang="swift">
 
// 2 dimensional array of arrays of Ints set to 0. Arrays size is 10x5
 
// 2 dimensional array of arrays of Ints set to 0. Arrays size is 10x5
 
var arr = Array(count: 3, repeatedValue: Array(count: 2, repeatedValue: 0))
 
var arr = Array(count: 3, repeatedValue: Array(count: 2, repeatedValue: 0))
Zeile 21: Zeile 23:
 
// ...and for Swift 3+:
 
// ...and for Swift 3+:
 
var arr = Array(repeating: Array(repeating: 0, count: 2), count: 3)
 
var arr = Array(repeating: Array(repeating: 0, count: 2), count: 3)
 +
</syntaxhighlight>
  
 
Change element at position
 
Change element at position
 
+
<syntaxhighlight lang="swift">
 
arr[0][1] = 18
 
arr[0][1] = 18
 
+
</syntaxhighlight>
 
OR
 
OR
 
+
<syntaxhighlight lang="swift">
 
let myVar = 18
 
let myVar = 18
 
arr[0][1] = myVar
 
arr[0][1] = myVar
 +
</syntaxhighlight>
  
 
Change sub array
 
Change sub array
 
+
<syntaxhighlight lang="swift">
 
arr[1] = [123, 456, 789]  
 
arr[1] = [123, 456, 789]  
 
+
</syntaxhighlight>
 
OR
 
OR
 
+
<syntaxhighlight lang="swift">
 
arr[0] += 234
 
arr[0] += 234
 
+
</syntaxhighlight>
 
OR
 
OR
 
+
<syntaxhighlight lang="swift">
 
arr[0] += [345, 678]
 
arr[0] += [345, 678]
  
Zeile 50: Zeile 54:
 
   [0, 0]
 
   [0, 0]
 
]
 
]
 +
</syntaxhighlight>
  
 
So be aware that sub arrays are mutable and you can redefine initial array that represented matrix.
 
So be aware that sub arrays are mutable and you can redefine initial array that represented matrix.
 
Examine size/bounds before access
 
Examine size/bounds before access
 
+
<syntaxhighlight lang="swift">
 
let a = 0
 
let a = 0
 
let b = 1
 
let b = 1
Zeile 60: Zeile 65:
 
     println(arr[a][b])
 
     println(arr[a][b])
 
}
 
}
 +
</syntaxhighlight>
  
 
Remarks: Same markup rules for 3 and N dimensional arrays.
 
Remarks: Same markup rules for 3 and N dimensional arrays.

Version vom 21. Dezember 2022, 15:20 Uhr

Multidimensionale Arrays

https://stackoverflow.com/questions/25127700/two-dimensional-array-in-swift

Define mutable array

// 2 dimensional array of arrays of Ints 
var arr = [[Int]]()

OR:

// 2 dimensional array of arrays of Ints 
var arr: [[Int]] = []

OR if you need an array of predefined size (as mentioned by @0x7fffffff in comments):

// 2 dimensional array of arrays of Ints set to 0. Arrays size is 10x5
var arr = Array(count: 3, repeatedValue: Array(count: 2, repeatedValue: 0))

// ...and for Swift 3+:
var arr = Array(repeating: Array(repeating: 0, count: 2), count: 3)

Change element at position

arr[0][1] = 18

OR

let myVar = 18
arr[0][1] = myVar

Change sub array

arr[1] = [123, 456, 789]

OR

arr[0] += 234

OR

arr[0] += [345, 678]

If you had 3x2 array of 0(zeros) before these changes, now you have:

[
  [0, 0, 234, 345, 678], // 5 elements!
  [123, 456, 789],
  [0, 0]
]

So be aware that sub arrays are mutable and you can redefine initial array that represented matrix. Examine size/bounds before access

let a = 0
let b = 1

if arr.count > a && arr[a].count > b {
    println(arr[a][b])
}

Remarks: Same markup rules for 3 and N dimensional arrays.