| ... | @@ -1733,6 +1733,36 @@ test "array initialization with function calls" { | ... | @@ -1733,6 +1733,36 @@ test "array initialization with function calls" { |
| 1733 | } | 1733 | } |
| 1734 | {#code_end#} | 1734 | {#code_end#} |
| 1735 | {#see_also|for|Slices#} | 1735 | {#see_also|for|Slices#} |
| | 1736 | |
| | 1737 | {#header_open|Multidimensional Arrays#} |
| | 1738 | <p> |
| | 1739 | Mutlidimensional arrays can be created by nesting arrays: |
| | 1740 | </p> |
| | 1741 | {#code_begin|test|multidimensional#} |
| | 1742 | const std = @import("std"); |
| | 1743 | const assert = std.debug.assert; |
| | 1744 | |
| | 1745 | const mat4x4 = [4][4]f32{ |
| | 1746 | [_]f32{ 1.0, 0.0, 0.0, 0.0 }, |
| | 1747 | [_]f32{ 0.0, 1.0, 0.0, 1.0 }, |
| | 1748 | [_]f32{ 0.0, 0.0, 1.0, 0.0 }, |
| | 1749 | [_]f32{ 0.0, 0.0, 0.0, 1.0 }, |
| | 1750 | }; |
| | 1751 | test "multidimensional arrays" { |
| | 1752 | // Access the 2D array by indexing the outer array, and then the inner array. |
| | 1753 | assert(mat4x4[1][1] == 1.0); |
| | 1754 | |
| | 1755 | // Here we iterate with for loops. |
| | 1756 | for (mat4x4) |row, row_index| { |
| | 1757 | for (row) |cell, column_index| { |
| | 1758 | if (row_index == column_index) { |
| | 1759 | assert(cell == 1.0); |
| | 1760 | } |
| | 1761 | } |
| | 1762 | } |
| | 1763 | } |
| | 1764 | {#code_end#} |
| | 1765 | {#header_close#} |
| 1736 | {#header_close#} | 1766 | {#header_close#} |
| 1737 | | 1767 | |
| 1738 | {#header_open|Vectors#} | 1768 | {#header_open|Vectors#} |