How Arrays Work — API Cheat Sheet
No exercise here — just how to declare, index, iterate, and manipulate raw arrays (and 2D grids) across Java, C#, Python, and JavaScript/TypeScript.
This is a reference page, not a graded exercise. Arrays are the one data structure that shows up in nearly every interview problem, so fumbling the basic syntax costs you time you don't have.
Java — int[] / T[]
| Want to... | Syntax | Example |
|---|---|---|
| Declare fixed-size | new int[n] | int[] arr = new int[5]; (zero-filled) |
| Declare with values | array literal | int[] arr = {1, 2, 3}; |
| Get length | .length (field, no parens) | arr.length |
| Read/write an index | [] | arr[0] = 5; |
| Sort | Arrays.sort(arr) | ascending, in place |
| Copy | Arrays.copyOf(arr, n) | arrays.copyOfRange(arr, from, to) for a slice |
| Fill with a value | Arrays.fill(arr, v) | Arrays.fill(arr, -1) |
| Print for debugging | Arrays.toString(arr) | arr.toString() alone prints garbage |
| 2D grid | new int[rows][cols] | zero-filled; access grid[r][c] |
| Note | Fixed size — no push/pop. Use ArrayList (see the Lists cheat sheet) when size changes. |
C# — int[] / T[]
| Want to... | Syntax | Example |
|---|---|---|
| Declare fixed-size | new int[n] | int[] arr = new int[5]; |
| Declare with values | array literal | int[] arr = {1, 2, 3}; |
| Get length | .Length (property, no parens) | arr.Length |
| Read/write an index | [] | arr[0] = 5; |
| Sort | Array.Sort(arr) | ascending, in place |
| Copy | (int[])arr.Clone() | or Array.Copy(src, dst, n) |
| Fill with a value | Array.Fill(arr, v) | Array.Fill(arr, -1) |
| Print for debugging | string.Join(", ", arr) | |
| 2D grid | new int[rows, cols] (rectangular) or int[rows][] (jagged) | grid[r, c] vs grid[r][c] — don't mix these up |
| Note | Fixed size. Use List<T> when size changes. |
Python — list (doubles as the array type)
| Want to... | Syntax | Example |
|---|---|---|
| Declare fixed-size, zero-filled | list multiplication | arr = [0] * n |
| Declare with values | list literal | arr = [1, 2, 3] |
| Get length | len(arr) | builtin, not a method |
| Read/write an index | [] | arr[0] = 5 |
| Sort | arr.sort() (in place) / sorted(arr) (new list) | arr.sort(reverse=True) |
| Slice / copy | slicing | arr[1:3], arr[:] copies the whole list |
| Grow / shrink | .append(v), .pop(), .insert(i, v), .remove(v) | Python lists are dynamic, not fixed-size like Java/C# arrays |
| 2D grid | list comprehension | grid = [[0] * cols for _ in range(rows)] — never [[0]*cols]*rows, that repeats the same inner list |
JavaScript / TypeScript — Array / T[]
| Want to... | Syntax | Example |
|---|---|---|
| Declare, zero-filled | Array(n).fill(0) | new Array(5).fill(0) |
| Declare with values | array literal | const arr = [1, 2, 3]; |
| Get length | .length (property, no parens) | arr.length |
| Read/write an index | [] | arr[0] = 5; |
| Sort | .sort() (in place!) | arr.sort((a, b) => a - b) — default sort is lexicographic, always pass a comparator for numbers |
| Slice / copy | .slice(start, end) | arr.slice() copies the whole array |
| Grow / shrink | .push(v), .pop(), .unshift(v), .shift(), .splice(i, count, ...items) | push/pop are O(1); shift/unshift are O(n) |
| Transform | .map(), .filter(), .reduce() | return new arrays, don't mutate |
| 2D grid | nested Array.from | Array.from({ length: rows }, () => Array(cols).fill(0)) |
The gotcha that costs people the most time: in Java and C#, arrays are fixed-size — there's no push/pop, you'd need a List/ArrayList for that. In Python and JS, what's called an "array" in interview-speak is actually a dynamic list — resizing is free. Know which one your language gives you before you reach for .append/.push and it doesn't exist.
Hints
Companies
No companies reported yet.
Discussion
Sign in to join the discussion.
Loading discussion...
Test results
This question doesn't have a code exercise.