![](/img/trans.png)
[英]When splitting around whitespace (\s+) the first index of the first resulting string has an empty 0th char
[英]0th index skipped when comparing elements between 2 sorted list
我是Java新手,正在使用以下循环(使用Eclipse)比较排序列表中的元素。
//Test Code
public static boolean isPermutation(String sIn1, String sIn2){
Boolean test = false;
if (sIn1.length() != sIn2.length()) {
test = false;
return test;
} else {
List<Character> a1 = new ArrayList<>();
List<Character> a2 = new ArrayList<>();
for (int i = 0; i < sIn1.length(); i++){
a1.add(sIn1.charAt(i));
}
for (int i = 0; i < sIn2.length(); i++){
a2.add(sIn2.charAt(i));
}
Collections.sort(a1);
Collections.sort(a2);
System.out.println(a1);
System.out.println(a2);
for (int x = 0; x < a1.size(); x++) {
if (a1.get(x) == a2.get(x)) {
System.out.println(x);
test = true;
} else {
test = false;
if (test = false) {
break;
}
}
}
}
System.out.println(test);
return test;
}
//Test Case
Pset1.isPermutation("abcd", "bcdA"));
//Sorted List
[a, b, c, d]
[A, b, c, d]
仅对包含4个元素的列表运行(1),我得到的输出是:
0 1 2 3
而运行(2)时,我得到:
1 2 3
x是此循环的局部变量,在其他任何地方均未声明。 我不明白为什么我在运行x时跳过x = 0(我想从列表中的第0个索引元素开始比较)。 有人可以解释一下吗?
谢谢!
编辑:添加了完整的代码和测试用例。
您的输入:
- [A B C D]
- [A B C D]
未打印0,因为字符“ a”和“ A”不相等。 如果需要区分大小写的解决方案,则应执行以下操作:
不区分大小写:
test = true;
for (int x = 0; x < a1.size(); x++) {
if (a1.get(x).LOWERCASE_LETTER == a2.get(x).LOWERCASE_LETTER) {
System.out.println(x);
} else {
test = false;
break;
}
}
另外,if语句中的条件也不正确( 将Operator分配为而不是equal ),并且它永远不会中断for循环。 我认为if语句是不必要的,因为它永远不会为假。
如果比较两个对象,我建议使用equals方法或比较器,也可以使用泛型。
比较器示例:
public static boolean isPermutation(String sIn1, String sIn2, Comparator<Character> comp) {
/* your code */
test = true;
for (int x = 0; x < a1.size(); x++) {
if (comp.compare(a1.get(x), a2.get(x)) == 0) {
System.out.println(x);
} else {
test = false;
break;
}
}
}
public static void main(String[] args) {
//Test Case
Pset1_WithComparator.isPermutation("abcd", "bcdA", new Comparator<Character>() {
@Override
public int compare(Character o1, Character o2) {
return o1.LOWERCASE_LETTER - o2.LOWERCASE_LETTER;
}
});
}
比较器和通用示例
public static <T extends Comparable<T>> boolean isPermutation(List<T> list1, List<T> list2, Comparator<T> comp) {
Boolean test = false;
if (list1.size() != list2.size()) {
test = false;
} else {
Collections.sort(list1);
Collections.sort(list2);
System.out.println(list1);
System.out.println(list2);
test = true;
for (int x = 0; x < list2.size(); x++) {
if (comp.compare(list2.get(x), list2.get(x)) == 0) {
System.out.println(x);
} else {
test = false;
break;
}
}
}
System.out.println(test);
return test;
}
public static void main(String[] args) {
//Test Case
List<Character> l1 = new ArrayList<Character>(Arrays.asList(new Character[]{'a','b','c','d'}));
List<Character> l2 = new ArrayList<Character>(Arrays.asList(new Character[]{'A','b','c','d'}));
Pset1_WithComparatorAndGenerics.isPermutation(l1, l2, new Comparator<Character>() {
@Override
public int compare(Character o1, Character o2) {
return o1.LOWERCASE_LETTER - o2.LOWERCASE_LETTER;
}
});
}
for (int x = 0; x < a1.size(); x++) {
if (a1.get(x) == a2.get(x)) {
//System.out.println(x);
test = true;
} else {
test = false;
// changed from test = false
if (test == false) {
break;
}
}
}
由于x = 0,System.out.println(x)不会打印0,因为它进入了else循环。 由于条件执行不正确,因此无法正确地逃避(“破坏”)for循环。
循环未输出0的原因是因为A
不等于a
,但是在下一个循环中,您用错误的方式将test
与布尔值进行比较,应该使用test == false
而不是test = false
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.