organize the docs and some rewording

This commit is contained in:
Andrew Kelley 2019-07-16 13:13:21 -04:00
parent 475a181028
commit 23dd7f4527
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9

View File

@ -1731,29 +1731,38 @@ test "array initialization with function calls" {
assert(more_points[4].y == 6); assert(more_points[4].y == 6);
assert(more_points.len == 10); assert(more_points.len == 10);
} }
{#code_end#}
{#see_also|for|Slices#}
// Multidimensional arrays are declared by simply adding another array before the existing array {#header_open|Multidimensional Arrays#}
var mat4x4 = [4][4]f32{ <p>
Mutlidimensional arrays can be created by nesting arrays:
</p>
{#code_begin|test|multidimensional#}
const std = @import("std");
const assert = std.debug.assert;
const mat4x4 = [4][4]f32{
[_]f32{ 1.0, 0.0, 0.0, 0.0 }, [_]f32{ 1.0, 0.0, 0.0, 0.0 },
[_]f32{ 0.0, 1.0, 0.0, 1.0 }, [_]f32{ 0.0, 1.0, 0.0, 1.0 },
[_]f32{ 0.0, 0.0, 1.0, 0.0 }, [_]f32{ 0.0, 0.0, 1.0, 0.0 },
[_]f32{0.0, 0.0, 0.0, 1.0} [_]f32{ 0.0, 0.0, 0.0, 1.0 },
}; };
test "multidimensional arrays" { test "multidimensional arrays" {
// Multidimensional arrays can be accessed as expected from other languages... // Access the 2D array by indexing the outer array, and then the inner array.
assert(mat4x4[1][1] == 1.0); assert(mat4x4[1][1] == 1.0);
// or iterated over like any other array // Here we iterate with for loops.
for (mat4x4) |row, rowNum| { for (mat4x4) |row, row_index| {
for (row) |column, colNum| { for (row) |cell, column_index| {
if (rowNum == colNum) { if (row_index == column_index) {
assert(column == 1.0); assert(cell == 1.0);
} }
} }
} }
} }
{#code_end#} {#code_end#}
{#see_also|for|Slices#} {#header_close#}
{#header_close#} {#header_close#}
{#header_open|Vectors#} {#header_open|Vectors#}