krahets/hello-algo · error · RangeError
Index Out Of Bounds Exception
Error message
Index Out Of Bounds Exception
What it means
Thrown by GraphAdjMat.removeVertex() (a RangeError) when index >= this.size() (i.e. >= vertices.length). Note the guard is asymmetric: it checks the upper bound only and does NOT reject negative index, so a negative index will slip through to Array.splice and be interpreted as an offset-from-end — a latent bug rather than a thrown error for negatives. The intent is to block deletion of a non-existent vertex position.
Source
Thrown at codes/typescript/chapter_graph/graph_adjacency_matrix.ts:52
const n: number = this.size();
// 向顶点列表中添加新顶点的值
this.vertices.push(val);
// 在邻接矩阵中添加一行
const newRow: number[] = [];
for (let j: number = 0; j < n; j++) {
newRow.push(0);
}
this.adjMat.push(newRow);
// 在邻接矩阵中添加一列
for (const row of this.adjMat) {
row.push(0);
}
}
/* 删除顶点 */
removeVertex(index: number): void {
if (index >= this.size()) {
throw new RangeError('Index Out Of Bounds Exception');
}
// 在顶点列表中移除索引 index 的顶点
this.vertices.splice(index, 1);
// 在邻接矩阵中删除索引 index 的行
this.adjMat.splice(index, 1);
// 在邻接矩阵中删除索引 index 的列
for (const row of this.adjMat) {
row.splice(index, 1);
}
}
/* 添加边 */
// 参数 i, j 对应 vertices 元素索引
addEdge(i: number, j: number): void {
// 索引越界与相等处理
if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {
throw new RangeError('Index Out Of Bounds Exception');View on GitHub (pinned to 69932aed18)
Solutions
- Validate both bounds yourself: if (index >= 0 && index < graph.size()) graph.removeVertex(index).
- Treat the vertex index as a positional cursor that shifts after every removeVertex — recompute or iterate carefully when removing multiple vertices (remove highest index first).
- If you mean to remove by value, find its index via graph.vertices.indexOf(val) and confirm >= 0.
- Fix the class guard to also reject index < 0 if you control the source.
Example fix
// before
graph.removeVertex(index); // throws if index >= size
// after
if (index >= 0 && index < graph.size()) graph.removeVertex(index);
// when removing several, go high-to-low to keep indices stable
indices.sort((a, b) => b - a).forEach(i => { if (i < graph.size()) graph.removeVertex(i); }); Defensive patterns
Strategy: validation
Validate before calling
function safeRemoveVertex(graph, index) {
// guard BOTH bounds: the class only checks the upper bound
if (index >= 0 && index < graph.size()) { graph.removeVertex(index); return true; }
return false;
} Type guard
null
Try / catch
null
Prevention
- The class guard omits index < 0; validate the lower bound yourself.
- When removing multiple vertices, process highest index first so lower indices stay valid.
- Map vertex values to indices via indexOf and confirm >= 0.
- Treat vertex index as positional — it shifts after every removeVertex.
When it happens
Trigger: Calling removeVertex(index) with index >= number of vertices; calling on a graph that was shrunk below a stale index captured earlier. A negative index does not throw here but instead splices from the end — still a logic error, just silent.
Common situations: Using a stale vertex count after prior removeVertex calls shrank the array; off-by-one loop using <= size; confusing vertex value with vertex index (the matrix is indexed by position, not by the stored value).
Related errors
- Index Out Of Bounds Exception
- Illegal Argument Exception
- Index out of bounds
- Illegal Argument Exception
- Illegal Argument Exception
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/72f26281c2b10bcd.
Report an issue: GitHub.