Java programming (BASICS)
Content: Chapter 6 -- Array
The selectionsort method in the program list 6-8 was not read at the time. I typed the code myself and did not expect an error: max = 0.
Package java06;
Public class selectionsort {
Public static void main (string [] ARGs ){
Printarray (selectionsort (New Double [] {-2,-3,-1}); // test sentence
System. Out. println (); // empty row
Printarray (selectionsort (New Double [] {2, 1, 4 }));
}
Public static double [] selectionsort (double [] list ){
For (INT I = List. Length-1; I> = 1; I --) {// a descending queue needs to be compared and arranged
// Double max = 0; // This is a bug,
// The output result is
///-3.0 0.0 0.0
// 1.0 2.0 4.0
// Values of Max and Min cannot be assigned with values such as 0. Three negative numbers cannot be compared with max (0. Therefore, you need to change it to a visible number in the number group. Now I choose list [0]. I don't know if it is appropriate ......
Double max = list [0]; // changed it to this (later I found that the original question in the book is actually this)
Int indexofmax = 0;
For (Int J = 0; j <= I; j ++) {// find the maximum value of the current row
If (max <list [J]) {
Max = list [J];
Indexofmax = J;
}
}
// Convert the array element with the maximum value and the last exchange value
List [indexofmax] = list [I];
List [I] = max;
}
}
Return list;
}
Public static void printarray (double [] list ){
For (INT I = 0; I <list. length; I ++)
System. Out. Print (list [I] + "");
}
}
At present, I can only get stuck with the basic knowledge all day long. I mean it is so bad now!