Java 按字母顺序排列字符串(不使用compareTo方法)
我试图在不使用Java 按字母顺序排列字符串(不使用compareTo方法),java,string,sorting,while-loop,compareto,Java,String,Sorting,While Loop,Compareto,我试图在不使用compareTo()的情况下对字符串数组进行排序,但在执行循环时,我陷入了的困境。有没有一种方法可以不使用compareTo()或Arrays.sort()对字符串进行字母排序 我使用建议的想法添加了这个方法,但是我不确定我将放置什么来运行字符串的索引 int alphabetize(String a, String b) { String A = a.toLowerCase(); String B = b.toLowerCase(); i
compareTo()
的情况下对字符串数组进行排序,但在执行循环时,我陷入了的困境。有没有一种方法可以不使用compareTo()
或Arrays.sort()对字符串进行字母排序
我使用建议的想法添加了这个方法,但是我不确定我将放置什么来运行字符串的索引
int alphabetize(String a, String b)
{
String A = a.toLowerCase();
String B = b.toLowerCase();
if (A < B)
{
return -1;
}
else if (A > B)
{
return 1;
}
else
{
}
}
int字母顺序(字符串a、字符串b)
{
字符串A=A.toLowerCase();
字符串B=B.toLowerCase();
if(AB)
{
返回1;
}
其他的
{
}
}
用以下内容替换您的while:
while(theFollower != 0 && ar[theFollower].compareTo(ar[theFollower - 1]) < 0)
while(theFollower!=0&&ar[theFollower]。比较(ar[theFollower-1])<0)
不能将对象与基本运算符进行比较compareTo
就是在这里完成这项工作的
我假设这是一个家庭作业,因为显而易见的方法就是使用compareTo()
,所以我将给您一个提示,告诉您如何编写自己的方法来进行比较。你想要签名的吗
int compareStrings(String s, String t);
返回-1
或0
或1
,具体取决于s
在字母表中是按字母顺序在t
之前还是之后
逐个字符检查两个字符串
s,在每个阶段,如果s
中的字符小于t
(这里您可以使用我通过创建另一个函数来依次比较每个字符来完成此程序。当发现两个字符不相等时,它会中断。例如,“Tony”和“Toby”,在比较“t”和“o”之后,它比较“n”和“b”,使“Toby”小于“Toby”,然后断开。对于包含在另一个字符串(如“Max”和“Maxwell”)中的字符串,它将较短的字符串设置为较小的字符串
在我的程序中,我要求用户输入名称,然后按字母降序排序。函数返回第一个字符串是否小于(不是“你不能在对象上使用
,只能在原语上使用。你对没有compareTo
的情况下算法应该是什么样子有什么想法吗?这是一个家庭作业,不允许使用库类和函数吗?这正是compareTo的用途:知道对象是否伟大r或小于另一个。为什么不使用compareTo()?是
的替代品。您完全可以自己重新实现此功能,但为什么要重新发明轮子呢?@JBNizet对JDK中已有的东西进行编码是一个很好的编程练习。这意味着您在完成后有一个模型解决方案可供参考。@chiatic security我可以同意,但如果解决方案非常简单,只需使用
而不是compareTo(),没有太多的东西需要重新实现,我们不得不想知道比较的意义是什么。我想学习不使用。比较。我想你必须重写函数。看看String
源代码。基本上,你必须比较字符串,一个字符接一个字符。所以我是否会使用.charAt(0)?@desire了解到您将有一个索引i
,它在字符串中不断移动,您将看到.charAt(i)
从两个字符串
中选择。每次比较后,如果它们相等,则将i
增加1。不过,在每个阶段都要注意检查是否超出了字符串
的末尾。
while(theFollower != 0 && ar[theFollower].compareTo(ar[theFollower - 1]) < 0)
int compareStrings(String s, String t);
/**
* To sort twenty names in descending order using Bubble sort algorithm
* This program does not use the compareTo method of the String class
*/
import java.util.Scanner;
public class NameBubbleSort {
/*
* function to find whether first string comes before second one in
* alphabetical order
*/
static boolean lessThan(String s1, String s2) {
boolean flag = false;
int i, lim;
String shorter;
// converting strings to lower case
String first = s1.toLowerCase(), second = s2.toLowerCase();
// finding shorter string setting iteration limit to its length
if (first.length() <= second.length()) {
shorter = first;
lim = first.length();
} else {
shorter = second;
lim = second.length();
}
// comparing successively each character one by one if they are same
for (i = 0; i < lim; i++)
if (first.charAt(i) != second.charAt(i)) {
if (first.charAt(i) < second.charAt(i))
flag = true;
// breaks the loop since the characters are unequal
break;
}
// condition if one name is contained in another
// eg. Peter Steve and Peter Stevens
if (i == lim && first == shorter)
flag = true;
return flag;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String t;
String[] name = new String[20];
System.out.println("Enter 20 names: ");
for (int i = 0; i < 20; i++)
name[i] = in.nextLine();
for (int i = 0; i < 20; i++)
for (int j = 0; j < 19 - i; j++)
// compares two adjacent strings and exchanges them if not in
// correct order
if (lessThan(name[j], name[j + 1])) {
t = name[j];
name[j] = name[j + 1];
name[j + 1] = t;
}
// printing the names
for (String i : name) {
System.out.println(i);
}
in.close();
}
}