I posted a question yesterday regarding the sorting of indexes in an array. I was getting weird results, which were partly correct. I figured out the reason, but I don't know how to fix it.
I've declared an array to have a MAX number of indexes of 50. After reading data into a file, only 24 or so are filled with actual data, the rest are filled with 0's. When I go to print, all 50 indexes are listed, in ascending order. I can't figure out how to only print the indexes with data.
Here is the link to my question yesterday: Sorting double arrays into ascending order
Below is my code to the array declaration and initialization, sort loop, and printing. Any help would be great!
private double[] x;
x = new double[50];
int index, j = x.length - 1,double temp;
for (j = x.length - 1; j >= 0; j--) {
for (index = 0; index <= j - 1; index++) { //start for
if (x[index] > x[index + 1]) { //start if
temp = x[index];
x[index] = x[index + 1];
x[index + 1] = temp;
}
}
}
for (index = 0; index < x.length; index++) {
System.out.printf("%3d. \t\t%5.1f%%\n", (index + 1), x[index]);
}