Arrays
Zeus provides dynamic arrays that can grow and shrink at runtime.
Creating Arrays
let numbers: i32[] = new i32[]; // Empty arraylet preallocated: i32[] = new i32[100]; // Pre-allocate capacityBasic Operations
let arr: i32[] = new i32[];
// Add elementsarr.push(10);arr.push(20);arr.push(30); // arr = [10, 20, 30]
// Access elements (bracket notation or .get())let first: i32 = arr[0]; // 10let second: i32 = arr.get(1); // 20
// Modify elements (bracket notation or .set())arr[0] = 100;arr.set(1, 200); // arr = [100, 200, 30]
// Remove last elementlet last: i32 = arr.pop(); // Returns 30, arr = [100, 200]
// Check lengthlet len: i32 = arr.length; // 2Method Reference
Mutating Methods
| Method | Description |
|---|---|
push(value) | Add element to end |
pop() | Remove and return last element |
set(index, value) | Set element at index |
fill(value) | Fill all elements with value |
clear() | Remove all elements |
copy(source) | Copy elements from another array |
Non-Mutating Methods
These return new arrays without modifying the original:
| Method | Description |
|---|---|
concat(other) | Combine two arrays |
slice(start, end) | Extract sub-array (end exclusive) |
reverse() | Reverse element order |
Search Methods
| Method | Description |
|---|---|
indexOf(value) | First index of value, or -1 |
lastIndexOf(value) | Last index of value, or -1 |
includes(value) | Returns true if value exists |
find(value) | Returns element or default value |
findIndex(value) | Same as indexOf |
isEmpty() | Returns true if length is 0 |
Non-Mutating Operations
let arr: i32[] = new i32[];arr.push(1); arr.push(2); arr.push(3);
// concat - combine arrayslet other: i32[] = new i32[];other.push(4); other.push(5);let combined: i32[] = arr.concat(other); // [1, 2, 3, 4, 5]// arr is still [1, 2, 3]
// slice - extract portion (start inclusive, end exclusive)let middle: i32[] = combined.slice(1, 4); // [2, 3, 4]
// reverse - reverse orderlet reversed: i32[] = arr.reverse(); // [3, 2, 1]// arr is still [1, 2, 3]
// Chain operationslet result: i32[] = arr.reverse().slice(0, 2); // [3, 2]Searching
let arr: i32[] = new i32[];arr.push(10); arr.push(20); arr.push(30); arr.push(20);
arr.indexOf(20); // 1 (first occurrence)arr.lastIndexOf(20); // 3 (last occurrence)arr.indexOf(999); // -1 (not found)arr.includes(20); // truearr.includes(999); // falsearr.isEmpty(); // falseMulti-Dimensional Arrays
// 2D arraylet matrix: i32[][] = new i32[][];matrix.push(new i32[]);matrix.push(new i32[]);matrix[0].push(1); matrix[0].push(2);matrix[1].push(3); matrix[1].push(4);
let value: i32 = matrix[0][1]; // 2
// 3D arraylet cube: i32[][][] = new i32[][][];cube.push(new i32[][]);cube[0].push(new i32[]);cube[0][0].push(42);let v: i32 = cube[0][0][0]; // 42Object Arrays
class Point { public x: i32; public y: i32; constructor(x: i32, y: i32) { this.x = x; this.y = y; }}
let points: Point[] = new Point[];points.push(new Point(1, 2));points.push(new Point(3, 4));
let p: Point = points[0];let sum: i32 = p.x + p.y; // 3Practical Examples
Sum Elements
function sumArray(arr: i32[]): i32 { let sum: i32 = 0; for (let i: i32 = 0; i < arr.length; i++) { sum += arr[i]; } return sum;}Remove Element at Index
function removeAt(arr: i32[], index: i32): i32[] { let before: i32[] = arr.slice(0, index); let after: i32[] = arr.slice(index + 1, arr.length); return before.concat(after);}Matrix Class
class Matrix { public data: i32[][];
constructor(rows: i32, cols: i32) { this.data = new i32[][]; for (let i: i32 = 0; i < rows; i++) { this.data.push(new i32[cols]); } }
public get(row: i32, col: i32): i32 { return this.data[row][col]; }
public set(row: i32, col: i32, value: i32): void { this.data[row][col] = value; }}Bounds Checking
Zeus performs automatic bounds checking when reading from arrays. Accessing an index outside the valid range throws an IndexOutOfBoundsException:
let arr: i32[] = new i32[];arr.push(10);arr.push(20);
let safe: i32 = arr[0]; // 10 - validlet also_safe: i32 = arr[1]; // 20 - validlet oob: i32 = arr[5]; // Throws IndexOutOfBoundsException!Handling Bounds Errors
You can catch bounds errors using try-catch:
function safeGet(arr: i32[], index: i32): i32 { try { return arr[index]; } catch (e: Error) { log("Index out of bounds: " + e.message); return -1; // Return default value }}Safe Access Pattern
Check length before accessing:
function getOrDefault(arr: i32[], index: i32, defaultValue: i32): i32 { if (index < 0) { return defaultValue; } if (index >= arr.length) { return defaultValue; } return arr[index];}