easyArrays 0 views

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...SyntaxExample
Declare fixed-sizenew int[n]int[] arr = new int[5]; (zero-filled)
Declare with valuesarray literalint[] arr = {1, 2, 3};
Get length.length (field, no parens)arr.length
Read/write an index[]arr[0] = 5;
SortArrays.sort(arr)ascending, in place
CopyArrays.copyOf(arr, n)arrays.copyOfRange(arr, from, to) for a slice
Fill with a valueArrays.fill(arr, v)Arrays.fill(arr, -1)
Print for debuggingArrays.toString(arr)arr.toString() alone prints garbage
2D gridnew int[rows][cols]zero-filled; access grid[r][c]
NoteFixed size — no push/pop. Use ArrayList (see the Lists cheat sheet) when size changes.

C# — int[] / T[]

Want to...SyntaxExample
Declare fixed-sizenew int[n]int[] arr = new int[5];
Declare with valuesarray literalint[] arr = {1, 2, 3};
Get length.Length (property, no parens)arr.Length
Read/write an index[]arr[0] = 5;
SortArray.Sort(arr)ascending, in place
Copy(int[])arr.Clone()or Array.Copy(src, dst, n)
Fill with a valueArray.Fill(arr, v)Array.Fill(arr, -1)
Print for debuggingstring.Join(", ", arr)
2D gridnew int[rows, cols] (rectangular) or int[rows][] (jagged)grid[r, c] vs grid[r][c] — don't mix these up
NoteFixed size. Use List<T> when size changes.

Python — list (doubles as the array type)

Want to...SyntaxExample
Declare fixed-size, zero-filledlist multiplicationarr = [0] * n
Declare with valueslist literalarr = [1, 2, 3]
Get lengthlen(arr)builtin, not a method
Read/write an index[]arr[0] = 5
Sortarr.sort() (in place) / sorted(arr) (new list)arr.sort(reverse=True)
Slice / copyslicingarr[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 gridlist comprehensiongrid = [[0] * cols for _ in range(rows)]never [[0]*cols]*rows, that repeats the same inner list

JavaScript / TypeScript — Array / T[]

Want to...SyntaxExample
Declare, zero-filledArray(n).fill(0)new Array(5).fill(0)
Declare with valuesarray literalconst 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 gridnested Array.fromArray.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.