Witam napisałem taki krótki programik dla praktyki (ciągle się uczę):
public class Main {
public static void main(String[] args) {
int[] table = {9, 6, 2, 3, 8, 1, 4, 7, 5, 9, 15, 21, 698, 2, 7};
Sortable sort = new Sortable() {
public int[] sort(int[] table) {
boolean does = true;
int i = 0;
int length = table.length - 1;
while (does) {
if (i == length) {
i = 0;
length--;
continue;
}else if(length == 0) {
does = false;
continue;
}else if (table[i] > table[i+1]){
int temporary;
temporary = table[i];
table[i] = table[i+1];
table[i+1] = temporary;
i++;
continue;
} else {
i++;
}
}
return table;
}
};
System.out.println("Posortawana tablica: ");
for (int s: sort.sort(table)) {
System.out.print(s + " ");
}
}
}
Interface:
public interface Sortable {
public int[] sort(int[] table);
}
I moje pytanko brzmi:
W tym fragmencie "else if(length == 0)" jak użyłem 0 to wyskakuje błąd ArrayOutOfBoundsException. Ale jak użyje 1 to wszystko działa poprawnie, tylko dlaczego, wg. mnie z 0 powinno wszystko śmigać, ktoś pomoże?