我正在为我的java类编写代码。它要求使用给定的BubbleSort方法并编写compareTo方法来对数组进行排序。 我想使用名称按字母顺序对数组进行排序。
我正在尝试创建一个只有名称的数组,并按字母顺序对它们进行排序。
我在排序方面遇到了问题。 我收到两个错误, 1.在
Sorting.bubbleSort(array);
表示无法找到符号。
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相关的所有方法中,并清除了大部分&#34;非静态方法&#34;错误,但1仍然存在
public static void bubbleSort(Comparable[] array) {
Trees.bubbleSort(array, 0, array.length-1);
}
答案 0 :(得分:0)
转换:
public void bubbleSort(Comparable[] array, int start, int end)
为:
public static void bubbleSort(Comparable[] array, int start, int end)