Java 检查列表是否不区分大小写排序
对于我的Java简介类,我创建了一个方法,该方法接受这个列表并输出一个新的排序列表。现在我尝试创建一个方法来检查这个列表是否已排序(两者都不区分大小写)。我尝试用简单的方法使用这个.equals(this.sorted()),但它不起作用,因为当我比较一个已经排序的列表时,输出的是false而不是true。我需要一些指导。我不能仅仅使用递归来使用数组或循环Java 检查列表是否不区分大小写排序,java,list,recursion,Java,List,Recursion,对于我的Java简介类,我创建了一个方法,该方法接受这个列表并输出一个新的排序列表。现在我尝试创建一个方法来检查这个列表是否已排序(两者都不区分大小写)。我尝试用简单的方法使用这个.equals(this.sorted()),但它不起作用,因为当我比较一个已经排序的列表时,输出的是false而不是true。我需要一些指导。我不能仅仅使用递归来使用数组或循环 import tester.Tester; // to represent a list of Strings i
import tester.Tester;
// to represent a list of Strings
interface ILoString {
//prodces a new list, sorted in alphabetical order case insensitive
ILoString sort();
//helps sort() sort a list
ILoString sortHelp(String acc);
//determines whether this list is sorted in alphabetical order,
//in a case-insensitive way.
boolean isSorted();
}
// to represent an empty list of Strings
class MtLoString implements ILoString {
MtLoString(){}
//prodces a new list, sorted in alphabetical order case insensitive
public ILoString sort() {
return this;
}
//helps sort() sort a list
public ILoString sortHelp(String acc) {
return new ConsLoString(acc, this);
}
//determines whether this list is sorted in alphabetical order,
//in a case-insensitive way.
public boolean isSorted() {
return true;
}
}
// to represent a nonempty list of Strings
class ConsLoString implements ILoString {
String first;
ILoString rest;
ConsLoString(String first, ILoString rest){
this.first = first;
this.rest = rest;
}
//determines whether this list is sorted in alphabetical order,
//in a case-insensitive way.
public boolean isSorted() {
return this.equals(this.sort());
}
//prodces a new list, sorted in alphabetical order case insensitive
public ILoString sort() {
return this.rest.sort().sortHelp(this.first);
}
//helps to sort the names in this ConsLoString
public ILoString sortHelp(String acc) {
if ( acc.compareToIgnoreCase(this.first) < 0) {
return new ConsLoString(acc, this);
}
else {
return new ConsLoString(this.first, this.rest.sortHelp(acc));
}
}
}
// to represent examples for lists of strings
class ExamplesStrings{
ILoString mary = new ConsLoString("Mary ",
new ConsLoString("had ",
new ConsLoString("a ",
new ConsLoString("little ",
new ConsLoString("lamb.", new MtLoString())))));
ILoString marySorted = new ConsLoString("a ",
new ConsLoString("had ",
new ConsLoString("lamb.",
new ConsLoString("little ",
new ConsLoString("Mary ", new MtLoString())))));
// test the method sort for the lists of Strings
boolean testSort(Tester t){
return
t.checkExpect(this.mary.sort(),this.marySorted)&&
t.checkExpect(this.marySorted.sort(),this.marySorted);
}
//test the method isSorted
boolean testisSorted(Tester t){
return
t.checkExpect(this.mary.isSorted(),false)&&
t.checkExpect(this.marySorted.isSorted(),true);
}
}
导入tester.tester;
//表示字符串列表的步骤
接口字符串{
//生成一个新列表,按字母顺序排序,不区分大小写
ILoString sort();
//帮助排序()对列表进行排序
ILoString sortHelp(字符串acc);
//确定此列表是否按字母顺序排序,
//以不区分大小写的方式。
布尔isSorted();
}
//表示字符串的空列表
类MtLoString实现ILoString{
MtLoString(){}
//生成一个新列表,按字母顺序排序,不区分大小写
公共字符串排序(){
归还这个;
}
//帮助排序()对列表进行排序
公共字符串sortHelp(字符串acc){
返回新的conclostring(acc,this);
}
//确定此列表是否按字母顺序排序,
//以不区分大小写的方式。
公共布尔值isSorted(){
返回true;
}
}
//表示非空字符串列表的步骤
类ConsLoString实现ILoString{
先串;
绳托;
ConsLoString(先字符串,后字符串){
this.first=first;
这个。休息=休息;
}
//确定此列表是否按字母顺序排序,
//以不区分大小写的方式。
公共布尔值isSorted(){
返回this.equals(this.sort());
}
//生成一个新列表,按字母顺序排序,不区分大小写
公共字符串排序(){
返回this.rest.sort().sortHelp(this.first);
}
//帮助在此ConsLoString中对名称进行排序
公共字符串sortHelp(字符串acc){
如果(根据比较信号案例(本次首次)<0){
返回新的conclostring(acc,this);
}
否则{
返回新ConsLoString(this.first,this.rest.sortHelp(acc));
}
}
}
//表示字符串列表的示例的步骤
类示例字符串{
ILoString mary=新消费字符串(“mary”,
新ConsLoString(“had”,
新ConsLoString(“a”,
新ConsLoString(“小”,
新的ConsLoString(“lamb.”,新的mtslostring();
ILoString marySorted=新conclostring(“a”,
新ConsLoString(“had”,
新ConsLoString(“lamb.”,
新ConsLoString(“小”,
新目录(“玛丽”,新目录();
//测试字符串列表的排序方法
布尔测试排序(测试仪t){
返回
t、 checkExpect(this.mary.sort(),this.marySorted)&&
t、 checkExpect(this.marySorted.sort(),this.marySorted);
}
//测试方法分类
布尔验证(测试仪t){
返回
t、 checkExpect(this.mary.isSorted(),false)&&
t、 checkExpect(this.marySorted.isSorted(),true);
}
}
您可以尝试创建一个新列表,它是您检查的列表的排序版本。然后将排序后的列表与原始列表进行核对。如果它们相等,则原始列表已排序
List<String> myStings = new ArrayList<>();
myStings.add("Mary");
myStings.add("had");
myStings.add("a");
myStings.add("little");
myStings.add("lamb");
myStings = myStings.stream().map(x -> x.toLowerCase()).collect(Collectors.toList());
List<String> sortedList = myStings.stream().sorted().collect(Collectors.toList());
System.out.println("Unsorted list: " + myStings);
System.out.println("Sorted list: " + sortedList);
System.out.println("Was it a sorted list: " + myStings.equals(sortedList));
我认为你的问题太模糊了,无法得到任何答案。我认为你的代码中有许多单一的问题不太容易解决。因此,你的问题是如何使用递归而不是迭代方法实现
isSorted
?@Lothar是的,基本上没有ILoString
中的getFirst
方法,我发现很难找到解决方案。您提供的界面是否完整?仔细想想,它本质上是sortHelp
的实现,使用boolean
作为返回类型,并为方法中的两个条件返回相应的值。谢谢你的帮助,我有点困惑,我以为这就是我在返回这个时所做的。等于(this.sorted());我以为我正在将此列表与此列表的排序版本进行比较。请检查我在上面所做的更新。实际上,您没有使用List的Java实现,如Java.util.Collections
中所示。相反,您只是使用对象创建了一个列表,对象上的默认实现.equals()
检查内存分配。这解决了您的问题吗?请勾选左侧的复选框,将其标记为已解决。谢谢
@Override
public boolean equals(Object o) {
ConsLoString toCompare = (ConsLoString) o;
if(this.first == toCompare.first) {
if (this.rest != null && toCompare.rest != null) {
return this.rest.equals(toCompare.rest);
} else {
return Boolean.TRUE;
}
} else {
return Boolean.FALSE;
}
}