Okay, so habe ich den folgenden Code und egal, was es mir -1 zurückgibt. Ich möchte es so haben, dass, wenn die ID übereinstimmt, es zurückgibt und indexiert, aber wenn es nach dem Durchlaufen des gesamten Datensatzes nicht übereinstimmt, gibt es einen negativen zurück. Wohin gehe ich hier falsch:Java kehrt zurück
public class StudentCollection {
private String[] ids = new String[] {"Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty"}; // keeps identification numbers of students
private String [] names = new String[] {"Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty"};; // keeps the names of students
private int size = 0; // number of students currently in the collection
private int findIndex(String id) {
int noIndex = 1;
for (int i=0;i<ids.length;i++){
if((ids[i].equalsIgnoreCase(id))){
System.out.println("The index of this student is " +i);
}
else {
noIndex = -1;
System.out.println(noIndex);
break;}
}
return noIndex;
}
Was ist 'ids' in Ihrem Code? –
Hinweis: Wo setzen Sie noIndex auf den Wert, den Sie zurückgeben möchten? Wann solltest du brechen? Warum negieren Sie das Ergebnis von equalsIgnoreCase? – samgak