java 检查数组中的字符串是否按字母顺序排列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17624162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if Strings in an Array are in Alphabetical Order
提问by user2577950
I'm checking if the strings in my array are arranged in alphabetical order. My codechecker is saying that my code fails to account for some cases, but I'm really not sure how to change it.
我正在检查数组中的字符串是否按字母顺序排列。我的代码检查器说我的代码无法解释某些情况,但我真的不知道如何更改它。
EDIT: Apparently my code returns "true" when checking the array "cat ape dog zebra", which is clearly false.
编辑:显然我的代码在检查数组“cat ape dog zebra”时返回“true”,这显然是错误的。
public boolean isSorted()
{
boolean sorted = true;
for(int i = 0; i < list.size(); i++)
{
for(int j = i+1; j < list.size(); j++)
{
if (list.get(i).compareTo(list.get(j)) == 1)
{
sorted = false;
}
}
}
return sorted;
}
回答by Shamim Hafiz
if (list.get(i).compareTo(list.get(j)) == 1)
The above line is erroneous. The returned value would be positive and not strictly equal to 1.
上面的行是错误的。返回的值将是正的并且不严格等于 1。
Try changing to
尝试更改为
if (list.get(i).compareTo(list.get(j)) >0)
回答by amatellanes
I guess you're using a instance variable to save a list of String
. Try this code where I use Collections.sort()
:
我猜您正在使用实例变量来保存String
. 在我使用的地方试试这个代码Collections.sort()
:
public boolean isSorted() {
// Copies all of the elements from one list into another.
List<String> listSorted = new ArrayList<String>(list);
// Sorts the new list.
Collections.sort(listSorted);
// Check if both of list are equals.
return listSorted.equals(list);
}
回答by Ermir
It's far easier than it looks: just iterate over the list, checking if adjacent elements are in the right order. If all adjacent pairs are in order, then the whole list is.
这比看起来容易得多:只需遍历列表,检查相邻元素的顺序是否正确。如果所有相邻对都是有序的,那么整个列表都是。
public boolean isSorted()
{
for(int a=0;a<list.size()-1;a++)
{
if(list.get(a).compareTo(list.get(a+1))>0)
{
return false;
}
}
return true;
}
回答by Itay Maman
compareTo() returns a positive value (not necessarily 1) if the string on the left-hand-side is "greater" than the one on the right. Thus, you need to change the condition from == 1
to >= 1
.
如果左侧的字符串比右侧的字符串“大”,compareTo() 将返回一个正值(不一定是 1)。因此,您需要将条件从 更改== 1
为>= 1
。
Also, you don't need a second loop (j) that runs over all elements. You just need to compare two consecutive elements, as follows:
此外,您不需要在所有元素上运行的第二个循环 (j)。你只需要比较两个连续的元素,如下:
public boolean isSorted() {
for(int i = 1; i < list.size(); i++)
if (list.get(i).compareTo(list.get(i - 1)) >= 1)
return false;
return true;
}
回答by Stephan
Using String.compareTo()
is a very easy way to do it.
使用String.compareTo()
是一种非常简单的方法。
String.compareTo() returns a negative number if the string precedes the argument of the method, 0 if it is the same, and a positive number if the string comes after the argument of the method
String.compareTo() 如果字符串在方法参数之前返回负数,如果相同则返回 0,如果字符串在方法参数之后返回正数
The way you did it:
你这样做的方式:
if (list.get(i).compareTo(list.get(j)) == 1)
Is very close, but it should be
非常接近,但应该是
if (list.get(i).compareTo(list.get(j)) > 0)
You could use that alongside of a comparator to quickly sort, or in your case check to see if it is sorted
您可以将它与比较器一起使用来快速排序,或者在您的情况下检查它是否已排序
boolean isSorted(String[] words) {
for (int i = 0; i < words.length()-1; i++) {
if (words[i].compareTo(words[i+1] >= 0) {
return false;
}
}
return true;
}
Or if you wanted to sort them, this would work:
或者,如果您想对它们进行排序,这将起作用:
Collections.sort(fooList,
new Comparator<String>()
{
public int compare(String s1, String s2)
{
return s1.compareTo(s2);
}
});
Or to return true or false
或返回真或假
回答by Rik
I know it's a Java question, but here's how I ended up implementing this in Kotlin very succinctly:
我知道这是一个 Java 问题,但这是我最终在 Kotlin 中非常简洁地实现它的方式:
myList.zipWithNext { a, b ->
if (a > b) {
fail("Expected a sorted list, but $a > $b")
}
}