这是我第一次为字符串写一个冒泡排序,显然我遇到了很多错误而程序无法运行。我不知道如何解决它。我的代码是:
import java.util.*;
public class SortingRecord{
public static void main(String args[]){
Scanner kb = new Scanner(System.in);
System.out.println("How many people?");
int n = Integer.parseInt(kb.nextLine());
Record[] records = new Record[n];
for(int i = 0; i<n; i++){
System.out.println("Inputting record["+i+"]:");
System.out.print("Please input <First Name>:");
String firstName = kb.nextLine();
System.out.println("Please input <Last Name>:");
String lastName = kb.nextLine();
records[i] = new Record(firstName, lastName);
}
sort(records);
System.out.println("----------------");
System.out.println("Print name in dictinary order:");
for(int i = 0; i < n ; i++)
System.out.println();
}
public static void sort(Record[] records){
if (records == null || records.length <= 1) return;
int n = records.length;
for(int i = 0; i< records.length ; i++){
for(int j = i+1 ; j< records.length; j++){
无法找到符号方法compareTo(Record)。
if(records[j] .compareTo(records[i]) < 0){
它说Record无法转换为java.lang.String
String temp = records[i];
records[i] = records[j];
records[j] = temp;
}
}
System.out.println(records[i]);
}
}
}
class Record{
public String firstName = "";
public String lastName = "";
public Record(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
}
答案 0 :(得分:1)
让我们来看看这个明显的错误:
if (records[j].compareTo(records[i]) < 0) {
Record
不提供任何compareTo
方法,因此您无法调用它 - 它不存在。
下一个错误:
String temp = records[i];
因为Record
不是String
的类型,因此无法分配,显而易见的解决方案是使用Record
,而不是...... < / p>
Record temp = records[i];
records[i] = records[j];
records[j] = temp;
好的,但我们如何解决compareTo
问题?这比听起来更复杂,当您实施Comparable
interface
(或直接实施compareTo
方法)时,我不会选择此路径。为什么?因为您可能想要更改对记录进行排序的方式,并且实现该方法会将您锁定为单个用例。
相反,我使用传入方法的Comparator
进行实际比较,为调用者提供了更改比较实际工作方式的灵活性
public static void sort(Record[] records, Comparator<Record> comparator) {
if (records == null || records.length <= 1) {
return;
}
int n = records.length;
for (int i = 0; i < records.length; i++) {
for (int j = i + 1; j < records.length; j++) {
if (comparator.compare(records[j], records[i]) < 0) {
Record temp = records[i];
records[i] = records[j];
records[j] = temp;
}
}
System.out.println(records[i]);
}
}
然后你可以做一些像......
sort(records, new Comparator<Record>() {
@Override
public int compare(Record o1, Record o2) {
return o1.firstName.compareTo(o2.firstName);
}
});
或
sort(records, new Comparator<Record>() {
@Override
public int compare(Record o1, Record o2) {
return o1.lastName.compareTo(o2.lastName);
}
});
甚至
sort(records, new Comparator<Record>() {
@Override
public int compare(Record o1, Record o2) {
int compare = o1.firstName.compareTo(o2.firstName);
if (compare == 0) {
compare = o1.lastName.compareTo(o2.lastName);
}
return compare;
}
});
或者您可能需要的其他组合才能满足您的要求
我建议您查看Comparator
了解更多详情
我还应该指出,您也可以使用Collections
对象,但是您需要将其转换为List
而不是数组...
Collections.sort(Arrays.asList(records), new Comparator<Record>() {...});
程序无法按字典顺序输出名称;(
对我来说很好......
import java.util.Comparator;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
Record[] records = new Record[] {
new Record("B", "B"),
new Record("C", "B"),
new Record("D", "B"),
new Record("A", "E"),
new Record("A", "B"),
new Record("A", "C"),
new Record("A", "A"),
};
sort(records, new Comparator<Record>() {
@Override
public int compare(Record o1, Record o2) {
int compare = o1.firstName.compareTo(o2.firstName);
if (compare == 0) {
compare = o1.lastName.compareTo(o2.lastName);
}
return compare;
}
});
for (Record record : records) {
System.out.println(record);
}
}
public static void sort(Record[] records, Comparator<Record> comparator) {
if (records == null || records.length <= 1) {
return;
}
for (int i = 0; i < records.length; i++) {
for (int j = i + 1; j < records.length; j++) {
if (comparator.compare(records[j], records[i]) < 0) {
Record temp = records[i];
records[i] = records[j];
records[j] = temp;
}
}
}
}
class Record {
public String firstName = "";
public String lastName = "";
public Record(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return firstName + " " + lastName;
}
}
}
输出
A A
A B
A C
A E
B B
C B
D B
答案 1 :(得分:0)
你在Record类中没有 compareTo 方法,这就是为什么找不到它:)你可能应该实现Comparable接口。< / p>
至于&#34;记录无法转换为java.lang.String&#34;,使用 toString 方法,你可以转换它,虽然你可能想要覆盖的toString 强>