Reputation: 63
I am writing a swift application so this question would be best answered in swift or any other language if the logic is the same.
I have a 2D array and I want to iterate through each even row as normal 0 - x however every odd row I would like to iterate in the opposite direction of x - 0.
I haven't included any code as it doesn't seem necessary. Any answers would be appreciated - code would be appreciated but isn't required I need to know the method to achieve this desired functionality.
Upvotes: 0
Views: 92
Reputation: 3867
let array: [[Int]] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for (r, row) in array.enumerated() {
for p in (r % 2 == 0 ? row : row.reversed()) {
print(p)
}
}
Upvotes: 1