Common Mistakes To Avoid

Understand the common mistakes and errors in order to avoid them.

❌ Common pitfalls

The following common mistakes should be avoided:

  • Forgetting to create the array and only declaring it (int[][] array;)
  • Using 11 as the first index instead of 00 while accessing rows and/or columns
  • Using array.length as the last valid row index instead of array.length - 1
  • Using array[0].length as the last valid column index instead of array[0].length - 1
  • Using array.length() instead of array.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 1200+ tech skills courses.