使用compareTo而不使用arraylist在Java上排序
我是Java的初学者,我一直在尝试使用compareTo方法对三个城市进行排序。我的三个测试词是Legion、LSD和Chunkey。为了识别错误,我用1-6之间的数字给出了每个输出可能性。对于输出1,它是(Chunkey,LSD,Legion),2=(Legion,LSD,Chunkey,4=(Chunkey,Legion,LSD)。如果我认为我遵循了正确的逻辑,即当比较两个字符串时,值为负值,则第一个值先出现,然后是第二个值,那么输出往往毫无意义。但即使这样做,它的顺序也不正确。请帮助!谢谢使用compareTo而不使用arraylist在Java上排序,java,string,comparison,Java,String,Comparison,我是Java的初学者,我一直在尝试使用compareTo方法对三个城市进行排序。我的三个测试词是Legion、LSD和Chunkey。为了识别错误,我用1-6之间的数字给出了每个输出可能性。对于输出1,它是(Chunkey,LSD,Legion),2=(Legion,LSD,Chunkey,4=(Chunkey,Legion,LSD)。如果我认为我遵循了正确的逻辑,即当比较两个字符串时,值为负值,则第一个值先出现,然后是第二个值,那么输出往往毫无意义。但即使这样做,它的顺序也不正确。请帮助!谢谢
import java.util.Scanner;
public class OrderingCity {
public static void main (String []args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the First City");
String c1 = input.nextLine();
System.out.println("Enter the Second City");
String c2 = input.nextLine();
System.out.println("Enter the Third City");
String c3 = input.nextLine();
//When the value is less than 0, the first value is first while the second value is second.
//When the first city is compared to the second and if it returns a negative value (<0) then
//the first city comes first and when the value of the second city is compared to the third
//city and it returns a negative value then the second city comes before the third city
if (c1.compareTo(c2) < 0 && c2.compareTo(c3) < 0){
System.out.println(c1 + " 1 " + c2 + " " + c3);
}
//When the first city is compared to the second and it returns a positive value (>0) then
//the second city comes first and when the value of the first city is compared to the third
//city and it returns a positive value (>0) then third city comes before the first.
else if (c1.compareTo(c2) > 0 && c1.compareTo(c3) > 0){
System.out.println(c2 + " 2 " + c3 + " " + c1);
}
else if (c2.compareTo(c3) < 0 && c3.compareTo(c1) < 0){
System.out.println(c2 + " 3 " + c3 + " " + c1);
}
else if (c2.compareTo(c3) > 0 && c2.compareTo(c1) > 0){
System.out.println(c3 + " 4 " + c1 + " " + c2 );
}
else if (c3.compareTo(c1) < 0 && c1.compareTo(c2) < 0){
System.out.println(c3 + " 5 " + c1 + " " + c2);
}
else if (c3.compareTo(c1) > 0 && c3.compareTo(c2) > 0) {
System.out.println(c1 + " 6 " + c2 + " " + c3);
}
}
}
import java.util.Scanner;
公共类排序城市{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“进入第一个城市”);
字符串c1=input.nextLine();
System.out.println(“进入第二个城市”);
字符串c2=input.nextLine();
System.out.println(“进入第三个城市”);
字符串c3=input.nextLine();
//当该值小于0时,第一个值为第一个值,第二个值为第二个值。
//将第一个城市与第二个城市进行比较时,如果返回负值(0),则
//当第一个城市的价值与第三个城市的价值相比较时,第二个城市排在第一位
//城市,它返回一个正值(>0),然后第三个城市位于第一个城市之前。
如果(c1.比较到(c2)>0和&c1.比较到(c3)>0){
系统输出打印LN(c2+“2”+c3+“”+c1);
}
else if(c2.比较到(c3)<0和&c3.比较到(c1)<0){
系统输出打印LN(c2+“3”+c3+“”+c1);
}
如果(c2.比较到(c3)>0和&c2.比较到(c1)>0){
系统输出打印LN(c3+“4”+c1+“”+c2);
}
如果(c3.比较到(c1)<0和&c1.比较到(c2)<0){
系统输出打印LN(c3+“5”+c1+“”+c2);
}
如果(c3.compareTo(c1)>0&&c3.compareTo(c2)>0){
系统输出打印LN(c1+“6”+c2+“”+c3);
}
}
}
将c1、c2和c3添加到数组列表并调用集合。按以下链接中的说明对其进行排序:
当您执行
c1.compareTo(c2) > 0 && c1.compareTo(c3) > 0
您没有检查与c3
这意味着(例如)“MMM”、“JJJ”、“CCC”
仅将MMM与JJJ进行比较,将MMM与CCC进行比较,而不将JJJ与CCC进行比较
因此,c2和c3的顺序被忽略这将有助于:
import java.util.Scanner;
public class OrderingCity {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the First City");
String c1 = input.nextLine();
System.out.println("Enter the Second City");
String c2 = input.nextLine();
System.out.println("Enter the Third City");
String c3 = input.nextLine();
String temp;
// Example: c b a
// c > b
if (c1.compareTo(c2) > 0) {
temp = c1;
c1 = c2;
c2 = temp;
}
// b c a
// c > a
if (c2.compareTo(c3) > 0) {
temp = c2;
c2 = c3;
c3 = temp;
}
// b a c
// a > b
if (c1.compareTo(c2) > 0) {
temp = c1;
c1 = c2;
c2 = temp;
}
// a b c
System.out.printf("%s %s %s", c1, c2, c3);
}
}
要简单地手动排序3个值,请使用中的迷你气泡排序逻辑 您确实说过“不使用arraylist”,但如果这不排除使用数组,那么这可能更简单:
String[] arr = { c1, c2, c3 };
Arrays.sort(arr);
System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);
但是,当您使用自然顺序()进行排序时,它会按Unicode(UTF-16)代码点进行排序,这意味着LSD
将在Legion
之前进行排序
您只需使用,即可获得所需的订购。对于阵列版本,请使用:
这将提供正确的输出:
Chunkey军团LSD
但是,这仍然只适用于普通英语。要按字母顺序在任何特定语言中排序,如书籍中的索引,您需要一个。您可以在冒泡排序逻辑中使用该方法,或将其指定给该方法:
例如:对于法语单词e touffer
、deuxième
和Hôtel
(带有重音的随机法语单词),排序如下:
Hôtel deuxième étouffer // Natural order
deuxième Hôtel étouffer // Case-insensitive order
deuxième étouffer Hôtel // Collator
我是否需要添加另一个&&compareTo以使其每行3个?如c1.compareTo(c2)>0&&c1.compareTo(c3)>0&&c2.compareTo(c3)<0是的,但看看用户2350659所说的,还有一个小气泡排序。
String[] arr = { c1, c2, c3 };
Arrays.sort(arr, Collator.getInstance());
System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);
Hôtel deuxième étouffer // Natural order
deuxième Hôtel étouffer // Case-insensitive order
deuxième étouffer Hôtel // Collator