我坚持完成一项任务,我应该编写两种方法,按升序和降序对数组进行排序。当我在其中一个方法上添加块注释时,它们都可以单独工作,但是当我同时使用它们时,我从两个方法中得到相同的结果。我已经获得了第一堂课,而且我不应该改变其中的任何内容。我写的第二节课,它是不能同时工作的sortAsc和sortDesc方法。非常感谢任何提示!
Exercise4a课程:
public class Exercise4a {
public static int[] parseString( String str ) {
String[] nbr = str.split( "," ); // Ex: strängen "13,4,7,9" blir arrayen {"13","4","7","9"}
int[] res = new int[ nbr.length ];
for( int i = 0; i < nbr.length; i++ ) {
res[ i ] = Integer.parseInt( nbr[i] );
}
return res;
}
public static void main(String[] args) {
String message = "";
String str = JOptionPane.showInputDialog( null, "Mata in heltal separerade med komma-tecken", "1,5,3,4,6,5,3,5,9,7,6");
int[] test1 = parseString( str );
int[] test2;
message += "Vektor: " + IntegerArrays.toString( test1 ) + "\n";
message += "Störst: " + IntegerArrays.max( test1 ) + "\n";
message += "Minst: " + IntegerArrays.min( test1 ) + "\n";
message += "Summa: " + IntegerArrays.sum( test1 ) + "\n";
message += "Medelvärde: " + String.format( "%1.2f", IntegerArrays.average( test1 ) ) + "\n";
message += "Range: " + IntegerArrays.range(test1) + "\n";
test2 = IntegerArrays.copy(test1);
message += "test2: " + IntegerArrays.toString( test2 ) + "\n";
IntegerArrays.sortAsc(test1);
IntegerArrays.sortDesc(test2);
message += "test1: " + IntegerArrays.toString( test1 ) + "\n";
message += "test2: " + IntegerArrays.toString( test2 ) + "\n";
JOptionPane.showMessageDialog( null, message );
}
}
IntegerArrays类:
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
public class IntegerArrays {
public static String toString(int[] array){
String res = "{";
for(int i = 0; i < array.length; i++){
res += array[i];
if(i < array.length-1){
res += ",";
}
}res += "}";
return res;
}
public static int max(int[] array){
int max = Integer.MIN_VALUE;
for(int i = 0; i < array.length; i++){
if(array[i] > max){
max = array[i];
}
}return max;
}
public static int min(int[] array){
int min = Integer.MAX_VALUE;
for(int i = 0; i < array.length; i++){
if(array[i] < min){
min = array[i];
}
}return min;
}
public static int sum(int[] array){
int sum = 0;
for(int i = 0; i < array.length; i++){
sum += array[i];
}
return sum;
}
public static double average(int[] array){
double average = 0;
int counter = 0;
for(int i = 0; i < array.length; i++){
average += array[i];
counter++;
}
return average/counter;
}
public static int range(int[] array){
int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
for(int i = 0; i < array.length; i++){
if(array[i] <= min ){
min = array[i];
}
else if(array[i] >= max ){
max = array[i];
}
}return max - min;
}
public static int[] sortAsc(int[] array){
// for(int i = 0; i < array.length; i++){
// for(int j = 0; j < array.length; j++){
// if(array[i] > array[j]){
// int temp;
// temp = array[i];
// array[i] = array[j];
// array[j] = temp;
//
// }
// }
// }return array;
Arrays.sort(array);
return array;
}
public static int[] sortDesc(int[] array){
for(int i = 0; i < array.length/2; i++){
int temp = array[i];
array[i] = array[array.length - i - 1];
array[array.length - i - 1] = temp;
}
return array;
}
public static int[] copy(int[] array){
int[] newArray = array;
return newArray;
}
}