java - 在多维数组中搜索值

时间:2014-12-04 23:58:01

标签: java arrays multidimensional-array

我需要搜索数组并找到某个值,如果它存在则返回true,如果不存在则返回false。

数组:

private String zonaDeInternamento[][][] = {
    {
        {"Livre", "Livre", "Livre", "Livre"},
        {"Livre", "Livre", "Livre", "Livre"},
        {"Livre", "Livre", "Livre", "Livre"},
        {"Livre", "0012", "Livre", "Livre"},
        {"Livre", "Livre", "Livre", "Livre"}
    },
    {
        {"Livre", "0013", "Livre", "Livre"},
        {"Livre", "Livre", "Livre", "Livre"},
        {"Livre", "Livre", "Livre", "Livre"},
        {"Livre", "Livre", "Livre", "Livre"},
        {"Livre", "Livre", "Livre", "Livre"}
    }
};

所以,如果搜索找到其中一个数字,则返回true,如果找到“Livre”,则返回false;

public boolean isPacienteInternado(String numeroProcesso) {
    if (isNumeroProcessoValido(numeroProcesso)) {
        for (int i = 0; i < zonaDeInternamento.length; i++) {
            for (int j = 0; j < zonaDeInternamento[i].length; j++) {
                for (int h = 0; h < zonaDeInternamento[i][j].length; h++) {
                    if ((zonaDeInternamento[i][j][h].equals(numeroProcesso))) {
                        System.out.println("O paciente com número de processo " + numeroProcesso + " está internado!");
                        return true;
                    } else {
                        System.out.println("O paciente com número de processo " + numeroProcesso + " não está internado!");
                        return false;
                    }
                }
            }
        }
    }
    return false;
}

它一直返回false,永远不会进入if语句,返回else消息和false。运行debug if((zonaDeInternamento [i] [j] [h] .equals(numeroProcesso)))当numeroProcesso为13或12时返回false。我缺少什么?

4 个答案:

答案 0 :(得分:1)

你在块的末尾给出了返回false,这导致错误在底部删除了返回false。

public boolean isPacienteInternado(String numeroProcesso) {
   bool value;
   if (isNumeroProcessoValido(numeroProcesso)) {
      for (int i = 0; i < zonaDeInternamento.length; i++) {
         for (int j = 0; j < zonaDeInternamento[i].length; j++) {
             for (int h = 0; h < zonaDeInternamento[i][j].length; h++) {
                 if ((zonaDeInternamento[i][j][h].equals(numeroProcesso))) {
                     System.out.println("O paciente com número de processo " + numeroProcesso + "                 está internado!");
                    bool = true;
                } else {
                    System.out.println("O paciente com número de processo " + numeroProcesso + " não está internado!");
                    bool = false;
                }
            }
        }
    }
}
return bool;

}

答案 1 :(得分:1)

public boolean isPacienteInternado(String numeroProcesso) {
if (isNumeroProcessoValido(numeroProcesso)) {
    for (int i = 0; i < zonaDeInternamento.length; i++) {
        for (int j = 0; j < zonaDeInternamento[i].length; j++) {
            for (int h = 0; h < zonaDeInternamento[i][j].length; h++) {
                if ((zonaDeInternamento[i][j][h].equals(numeroProcesso))) {
                    System.out.println("O paciente com número de processo " + numeroProcesso + " está internado!");
                    return true;
                }
            }
        }
    }
}
return false;}

答案 2 :(得分:1)

您只检查整个数组中的一个项目,如果它不等于numeroProcesso,那么您将退出循环。相反,您需要继续循环,直到 1。您找到了匹配 2。你迭代了数组中的每一个项目。而你想要这样的东西:

public boolean isPacienteInternado(String numeroProcesso) {
if (isNumeroProcessoValido(numeroProcesso)) {
    for (int i = 0; i < zonaDeInternamento.length; i++) {
        for (int j = 0; j < zonaDeInternamento[i].length; j++) {
            for (int h = 0; h < zonaDeInternamento[i][j].length; h++) {
                if ((zonaDeInternamento[i][j][h].equals(numeroProcesso))) {
                    System.out.println("O paciente com número de processo " + numeroProcesso + " está internado!");
                    return true;
                } 
            }
        }
    }
    // let the user know that a match was not found
    System.out.println("Combinar não encontrado");
}
return false;

}

答案 3 :(得分:1)

你的for循环只进行一次迭代,因为你在第一步中返回true或false。 所以删除else语句。

if ((zonaDeInternamento[i][j][h].equals(numeroProcesso))) {
   System.out.println("O paciente com número de processo " + numeroProcesso + " está internado!");
   return true;
}