I'm a newbie studying java and in my notes I found the following code (a code that I know is simple, but my head hasn't rationalized it yet, and I wanted to see if you could help me a little...):
public static Clasificable[] clasificar(Clasificable[] array) {
int i, j;
Clasificable c;
for (i = 0; i < array.length; i++) {
for (j = i + 1; j < array.length; j++) {
if (array[j].compare(array[i]) == Clasificable.INFERIOR) {
c = array[j];
array[j] = array[i];
array[i] = c;
} else if (array[j].compare(array[i]) == Clasificable.ERROR) {
return null;
}
}
}
return array;
}
Why do we create i
and j
exactly? Why would we add a 1 to the i
in the second for
? Yes, I know these are quite silly questions, but really my mind has not reached there yet, and I would like to understand it and get there, so if someone sees the need to explain it to me as a 4-year-old, I ask you to please Do it...
The value of LOWER, etc would be:
public static final int INFERIOR=-1;
public static final int IGUAL=0;
public static final int SUPERIOR=1;
public static final int ERROR=-99;