[英]Comparing character strings
So I have a method which compares two character arrays that are inserted (s and t where s is the character array which should return true if is smaller ) and the aim is to find out whether one is less than the other (in dictionary order) to the first n characters. 因此,我有一个比较插入的两个字符数组的方法(s和t,其中s是字符数组,如果更小则应返回true),目的是找出一个是否小于另一个(按字典顺序)到前n个字符。
public boolean lessCheck(char[] s, char[] t, int n) {
int q = 0;
if (t.length < n || s.length < n) {
if (s.length != t.length) {
if (t.length < s.length) {
q = t.length;
} else {
q = s.length;
}
} else {
q = t.length;
}
} else {
q = n;
}
for (int i = 0; i < q; i++) {
if (t[i] != s[i]) {
if (t[i] > s[i]) {
return true;
} else {
return false;
}
}
}
if (equal(s, t, n)) {
return false;
} else {
return true;
}
}
The issue that I'm having is that whenever I sub 我遇到的问题是,每当我
s = bind, t = bin and n = 7 I get true. s =绑定,t = bin,n = 7,我是对的。
This should return false as bind is less than bin in dictionary order... 这应该返回false,因为bind小于字典顺序中的bin。
Any help? 有什么帮助吗?
s = bind, t = bin and n = 7 I get true. s =绑定,t = bin,n = 7,我是对的。
when called q=3 当称为q = 3
right? 对?
the for loop compares the 3 chars b,i,n . for循环比较3个字符b,i,n 。
right? 对?
The final if statement uses the equal method. 最后的if语句使用equal方法。 Lenghts of bind and bin are different and lower than n. bind和bin的长度不同并且小于n。
equal return false, right? 等于返回假,对吗?
The final result you got is true, from the else block of the last if . 从最后一个if的else块获得的最终结果为true。
right? 对?
The String class defines a compareTo method which should met your requirements: String类定义了一个compareTo方法,该方法应满足您的要求:
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String) https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String)
new String("bind").compareTo("bin")>0
new String("bin").compareTo("zoo")<0
new String("bin").compareTo("bin")==0
The problem seems to be related to this piece of code: 问题似乎与这段代码有关:
for (int i = 0; i < q; i++) {
if (t[i] != s[i]) {
if (t[i] > s[i]) {
return true;
} else {
return false;
}
}
}
that should be 那应该是
for (int i = 0; i < q; i++) {
if (t[i] != s[i]) {
if (t[i] > s[i]) {
return true;
} else if (t[i] < s[i]) {
return false;
}
}
}
In fact, if the condition t[i] > s[i] is NOT true, you still need to check whether t[i] < s[i] in order to return false. 实际上,如果条件t [i]> s [i]不为真,则仍需要检查t [i] <s [i]是否为假。
I think a more compact version of your algorithm would be: 我认为您算法的一个更紧凑的版本是:
public static boolean lessThan(char[] s, char[] t, int n) {
int minLength = Math.min(Math.min(s.length, t.length), n);
for (int i = 0; i < minLength; i++) {
if (s[i] < t[i]) {
return true;
} else if (s[i] > t[i]) {
return false;
}
}
return (minLength < n && s.length < t.length) ? true : false;
}
I hope that helps! 希望对您有所帮助!
Your equals
method returns false
because the arrays are not the same length, then, because equals
returned false
, your lessThan
method returns true
. 您的equals
方法返回false
因为数组长度不同,然后,由于equals
返回false
,您的lessThan
方法返回true
。
There is no point in calling equals
method after you compared almost all the characters. 比较了几乎所有字符后,调用equals
方法毫无意义。 Instead of calling equals
method, just check if s is shorter than t: 而不是调用equals
方法,只需检查s是否小于t:
return s.length() < t.length();
If the subarrays are equal and one just shorter than the other, it means it will be placed before the longer array in the dictionary. 如果子数组相等并且一个子数组短于另一个子数组,则意味着它将被放置在字典中较长数组的前面。
Sorry couldn't resist the logic to find q. 对不起,无法抗拒找到q的逻辑。 There is no need to find this q. 无需找到此q。
try this: 尝试这个:
public boolean lessThan(char[] s, char[] t, int n) {
int i = 0, j = 0, q = 0;
while (i < s.length && i < t.length && (i < n)) {
if (s[i] > t[i])
return false;
i++;
}
if (s.length > t.length)
return false;
else if (n < i)
return true;
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.