Common Mistakes To Avoid
Understand the common mistakes and errors in order to avoid them.
We'll cover the following
❌ Common pitfalls
The following common mistakes should be avoided:
- Forgetting to create the array and only declaring it (
int[][] array;
) - Using as the first index instead of while accessing rows and/or columns
- Using
array.length
as the last valid row index instead ofarray.length - 1
- Using
array[0].length
as the last valid column index instead ofarray[0].length - 1
- Using
array.length()
instead ofarray.length
(not penalized on the free response) - Going out of bounds when looping through an array (using
index <= array.length
) - Using
array.length
for both the number of rows and columns
🚨 An important alert
-
Do not use initializer syntax, except when declaring the array variable. The following ways are valid:
Method 1:
int [][] arr = new int[][]{{1, 2}};
Method 2:
int [][] arr = {{1, 2}};
Method 3:
int [][] arr; arr = new int[][]{{1, 2}};
Get hands-on with 1400+ tech skills courses.