【问题标题】:How to sort an array using BubbleSort and compareTo in Java?如何在 Java 中使用冒泡排序和 compareTo 对数组进行排序?
【发布时间】:2016-02-24 11:46:05
【问题描述】:

我正在为我的 java 类编写代码。它要求使用给定的 BubbleSort 方法并编写一个 compareTo 方法来对数组进行排序。 我想使用名称按字母顺序对数组进行排序。

我正在尝试创建一个仅包含名称的数组并按字母顺序对它们进行排序。

我在排序时遇到问题。 我收到两个错误, 1.在

  Sorting.bubbleSort(array);

说找不到符号。

  1. public static void bubbleSort(Comparable[ ] array) {
    

错误提示:无法从静态上下文引用非静态方法 bubbleSort(Comparable[],int,int)

我不知道该尝试什么,因为我不确定这是否是正确的方法。

这是假设排序的主要部分,然后打印数组

   // bubbleSort
     Sorting [] Names = new Sorting [MAX];
     Sorting.bubbleSort(array);

    //use toString() to display the array again with updated data 
    System.out.println("\nDisplay Trees array[] after initializing elements:");
    System.out.println("index    name          height  diameter");
    for(int i=0; i<count; i++){
      System.out.printf("%-9d", i);
      System.out.println(array[i].toString());
    }    
}//end of main() method

这是第二类的一部分

 /**
 * toCompare() - cast String name to object 
 *    
 */ 
 public String compareTo(Object objName) {
    Trees name = (Trees) objName; 
 }

 /**bubble sort
 * @param array is an array of Comparable objects
 */
     public static void bubbleSort(Comparable[] array) {
     Trees.bubbleSort(array, 0, array.length-1);
    }

 /**bubble sort
 * @param array is an array of Comparable objects
 * @param start is the first element in the array   
 * @param end is the last element in the array */
     public void bubbleSort(Comparable[] array, int start, int end) {
    //flag to see if an item was swapped or not
       boolean swap = false;
      // loop size - 1 times   
       for (int i = start + 1; i <= end; i++) {
          swap = false;
          //loop from beginning of array to (last element - i)
          for (int j = 0; j <= end - i; j++) {
          // swap if 1st item greater than 2nd item
             if (array[j].compareTo(array[j + 1]) > 0) {
             // swap
                Comparable temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
                swap = true;
             }
          }
        //if no swap, array is in order
        //so break out of loop early
        if (!swap) {
           Trees.print(array, start, end);
           break;
        }
     }
  }

 /**prints out an array from start index to ending index
 * @param array is an array of Comparable objects   
 */
     public void print(Comparable[] array){
    //overloading: two methods with same name, but different   parameter type and/or count
    //calls print method with 3 parameters 
       Trees.print(array, 0, array.length-1);
    }

 /**prints out an array from start index to ending index
 * @param array is an array of Comparable objects   
 * @param start is the first element in the array   
 * @param end is the last element in the array  */  
     public void print(Comparable[] array, int start, int end) {
       for (int i = 0; i < array.length; i++) {
          if(i>=start && i<=end){
             System.out.print(array[i] + ", ");
          }
          else{
          //display blanks for proper placement of elements
             System.out.print("   ");
         }
       } 
       System.out.println();
    }

对不起,我知道这很多而且有点混乱。如果您需要任何澄清,我会尽力解释,因为我对这些材料不太确定。 谢谢你。

更新: 在 main() 方法中我改为

Comparable sortarray[] = new Comparable [MAX];
Trees.bubbleSort(sortarray);

它摆脱了第一个错误。然后我将静态添加到与bubbleSort相关的所有方法中,并清除了大部分“非静态方法”错误,但是1仍然存在

public static void bubbleSort(Comparable[] array) {
        Trees.bubbleSort(array, 0, array.length-1);
     }

【问题讨论】:

    标签: java arrays sorting


    【解决方案1】:

    转换:

    public void bubbleSort(Comparable[] array, int start, int end)
    

    到:

    public static void bubbleSort(Comparable[] array, int start, int end)
    

    【讨论】:

    • 我现在得到的唯一错误是带有 Comparable[ ] 数组参数的 bubbleSort 方法。我已将静态添加到与 bubbleSort 相关的所有方法中,并清除了其他“非静态方法”错误
    • Trees.bubbleSort(array, 0, array.length-1); ^ 注意:Sorting.java 使用未经检查或不安全的操作。注意:使用 -Xlint:unchecked 重新编译以获取详细信息。 1 个错误
    • @EmsandEms: 在你的函数上方添加@SuppressWarnings("unchecked") 看看它是否能解决你的问题
    • @EmsandEms 这个解决方案终于帮到你了吗?
    猜你喜欢
    • 2019-05-12
    • 2019-04-14
    • 2012-04-14
    • 2020-07-17
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 2018-09-22
    相关资源
    最近更新 更多