我正在尝试创建冒泡排序,使用的代码如下。
import java.util.*;
public class sorting implements Comparable<sorting> {
public void sort(String[][] Locations) {
for (int a = 0; a < Locations.length; a++)
for (int b = 0; b < Locations[a].length - 1; b++) {
for (int c = 0; c < Locations[a].length - b - 1; c++){
if (Locations[a][b].compareTo(Locations[a][b+1])>0){
String temp = Locations[a][b];
Locations[a][b] = Locations[a][b+1];
Locations[a][b+1] = temp;
}
}
}
}
}
此代码不仅不会根据字母顺序对位置进行排序,还会由于 Comparable 接口而引发错误。
起初我使用了compareTo()方法,没有实现接口,并且没有抛出错误,但是数组中的项目都没有排序。冒泡排序本身似乎绝对是正确的,所以我不认为这是问题所在。我尝试实现 Comparable,但随后它抛出一个错误,指出排序不是一个抽象类,并且它不会重写compareTo() 方法。我无法辨别compareTo()方法中的参数有什么问题。
我想你可以尝试这样的事情,看看它是否适合你的需要(阅读方法JavaDoc):
/**
* Sorts a 2D String Array in Ascending order based on the desired
* literal column number supplied.
*
* @param array (2D String Array) The 2D String Array to sort.<br>
*
* @param literalColumnNumber (Integer) This parameter must be provided as
* the <b>literal</b> column number you wish to sort, not the Index value
* of the column. If 0 is supplied then the Array Rows are sorted in ascending
* order instead of any columns. If this is the case then true does not need
* to be passed within the optional <b>sortRowsAlso</b> parameter.<br>
*
* @param sortRowsAlso (Boolean varArg - optional - Default is false) Sometimes you
* may want both the Rows and a particular column sorted. If you pass true to
* this optional parameter then the Rows will be sorted first then the column
* related to the supplied column number is sorted. The Array Rows will always
* be sorted first. If false is supplied then there will be no Row Sorting
* unless 0 is supplied within the <b>literalColumnNumber</b> parameter.<br>
*
* @return (2D String Array) This method will always sort the supplied 2D String
* Array therefore the return result is not really required however if you want
* to store the results into an entirely different 2D String Array then this
* returned result will do that for you. Here are some use case examples:<pre>
*
* <b>Example 1:</b>
*
* String[][] table = {
* {"z", "b", "v"},
* {"s", "w", "a"},
* {"r", "c", "h"}
* };
*
* // Sort the table Array
* bubbleSort2DStringArray(table, 3, true);
*
*
* <b>Example 2:</b>
*
* String[][] table = {
* {"z", "b", "v"},
* {"s", "w", "a"},
* {"r", "c", "h"}
* };
*
* // Sort the `table` Array and store within the `table_2` array.
* String[][] table_2 = bubbleSort2DStringArray(table, 3, true);
*
* The results for both examples if the 2D array is printed out is:
*
* c h r
* a s w
* b v z </pre>
*/
public static String[][] bubbleSort2DStringArray(String[][] array, int literalColumnNumber,
boolean... sortRowsAlso) {
boolean sortRows = false;
if (sortRowsAlso.length > 0) {
sortRows = sortRowsAlso[0];
}
int i = 0, j = 0;
int column = literalColumnNumber;
String[] temp = null;
boolean swap = true;
if (column == 0 || sortRows) {
for (i = 0; i < array.length; i++) {
temp = new String[array[i].length];
for (j = 0; j < array[i].length; j++) {
temp[j] = array[i][j];
}
int n = temp.length;
String temp2;
for (int k = 0; k < n; k++) {
for (int L = 1; L < (n - k); L++) {
if (temp[L - 1].compareTo(temp[L]) > 0) {
temp2 = temp[L - 1];
temp[L - 1] = temp[L];
temp[L] = temp2;
}
}
}
array[i] = temp;
}
}
if (column > 0) {
column = (column - 1);
while (swap) {
for (i = 0; i < array.length - 1; i++) {
swap = false;
if (array[i][column].compareTo(array[i + 1][column]) > 0) {
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
swap = true;
}
}
}
}
return array;
}