使用选择排序按区域对形状数组进行排序

时间:2018-12-05 19:42:52

标签: java arrays sorting

我一直在尝试实现一种排序算法,以按形状对Shape对象的数组进行排序,但我运气不佳。对列表进行排序时,我一直收到NullPointerException,但它拒绝打印。我对如何将排序算法应用于除整数以外的事物感到困惑。我对Java非常陌生,因此不胜感激。非常感谢你。

public class ShapeApp2 {

public static void sort(Shape arr[]){
    int n = arr.length;
    for (int i=0; i< n; i++){
        int min_area = i;
        for (int j = i+1; j < n-1; j++) 
            if (arr[j].getArea() < arr[min_area].getArea()) 
                min_area = j;
        Shape temp = arr[min_area]; 
        arr[min_area] = arr[i]; 
        arr[i] = temp; 

    }
}
 public static void printArray(Shape arr[]) 
{ 
    int n = arr.length; 
    for (int i=0; i<n-1; ++i) 
        System.out.print(arr[i].getDescription()+" "); 
    System.out.println(); 
}

/**
 * @param args the command line arguments
 * @throws java.lang.Exception
 */

public static void main(String[] args) {
     Shape[] test = new Shape[10];

     System.out.println("Choose a shape or type stop to break away?");
     Scanner sc = new Scanner(System.in);

     int n = test.length;
     for (int i=0; i<test.length; i++) {
     String Shape = sc.nextLine();
     switch (Shape) {
         case "Rectangle":
             System.out.println("You have chosen a Rectangle");
             test[i] = new Rectangle();
             System.out.println("Enter another one now");
             break;
         case "Square":
             System.out.println("You have chosen a Square");
             test[i] = new Square();
             System.out.println("Enter another one now");
             break;         
         case "Equilateral Triangle":
             System.out.println("You have chosen an Equilateral Triangle");
             test[i] = new Equilateral_Triangle();
             System.out.println("Enter another one now");
             break;
         case "Right Triangle":
             System.out.println("You have chosen a Right Triangle");
             test[i] = new Right_Triangle();
             System.out.println("Enter another one now");
             break;
         case "Isosceles Triangle":
             System.out.println("You have chosen an Isosceles Triangle");
             test[i] = new Isosceles_Triangle();
             System.out.println("Enter another one now");
             break;
         case "Scalene Triangle":
             System.out.println("You have chosen a Scalene Triangle");
             test[i] = new Scalene_Triangle();
             System.out.println("Enter another one now");
             break;
         case "Stop":
             System.out.println("Gotcha, here's your list, Goodbye!");
             sort(test);
             printArray(test);
             break;
         case "Print":
             sort(test);
             printArray(test);
             break;

      }
         if (test[i]==null){
             return;
         }
     }

}

}

1 个答案:

答案 0 :(得分:0)

我的猜测是,arr[j].getArea() < arr[min_area].getArea()在这里有一个空指针异常,因为尚未初始化数组中的所有元素。在调用此代码之前,请先进行空检查:

if ((arr[j] != null && arr[min_area]!= null) && (arr[j].getArea() < arr[min_area].getArea()))