我有这个数组:调用String [] [] datos = new String [99999] [3];我必须填写一些数据,好像它是一所学校。第一列是学生的身份证号码,第二列是姓名,第三列是他必须支付给学校的金额。您可能会注意到,通过首先询问ID号,您可以按第一列(ID号)排列它们。问题是我也希望通过第二列或第三列来安排它,并且由于表示尚未填充的id号的空值,我无法在没有NullPointerException的情况下找到一种方法。有人可以给我一个正确排序的方法来帮助我吗? 这是我要求的身份证号码:
System.out.println("Ingrese datos del alumno (apellido y nombre)");
datos[matricula][1]=in.readLine();
datos[matricula][0]=Integer.toString(matricula);
我告诉他们已经使用了身份证号码。
if(datos[matricula][0]!=null){
System.out.println("\n\nERROR.\nEsa matricula ya existe");
}
//Here I tell them to add the name:
else if(datos[matricula][0]==null){
System.out.println("Ingrese datos del alumno (apellido y nombre)");
datos[matricula][1]=in.readLine();
datos[matricula][0]=Integer.toString(matricula);
}
然后我将它发送到一个名为reportes的方法,我将在其中显示信息。 我使用此代码只是显示它们,因为它们是由id号排列的。
for(int i=0;i<=datos.length-1;i++){
if(datos[i][0]!=null){System.out.println(datos[i][0]+"\t\t\t"+datos[i][1]+"\t\t\t"+datos[i][2]);
}
答案 0 :(得分:0)
正如luisluix所说,您可以使用空字符串值(&#34;&#34;)启动矩阵内的所有字符串。 这是一个示例:
for(int r = 0; r < 99999; r ++)
{
for (int c = 0; c < 3; c ++)
{
datos[r][c] = "";
}
}