1. 程式人生 > 其它 >java.lang. ArrayIndexDut0fBoundsException: Index 0 out of bounds for length 0

java.lang. ArrayIndexDut0fBoundsException: Index 0 out of bounds for length 0

技術標籤:BUG人生

在這裡插入圖片描述
我是在力扣提交答案出現的這個問題,百思不得其解答,最後看的力扣的評論區才發現的,原來力扣測試用的是一個空陣列,而我沒有排除這種情況導致陣列索引越界了。
解決方法
加上下面的程式碼,先進行判斷陣列是否為空

 class Solution {
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
      if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return false
; } } }