英文:
Comparing a String Array to Array List
问题
目前,我有一个字符串数组:
String[] properSequence = ability.getSequence();
我想将它与一个ArrayList
ArrayList<String> sequence
目前我正在这样做:
boolean matchesSequence = true;
String[] properSequence = ability.getSequence();
int index = 0;
for(String s : sequence) {
String s1 = properSequence[index];
if(!s1.equalsIgnoreCase(s)) {
matchesSequence = false;
break;
}
index++;
}
if(matchesSequence) {
// 匹配时的代码
}
我想知道是否有更简单/更漂亮的方法来做这件事,看起来有点多余。
英文:
I currently have a String Array
String[] properSequence = ability.getSequence();
And I want to compare it to an ArrayList<String>
ArrayList<String> sequence
As of right now I'm doing,
boolean matchesSequence = true;
String[] properSequence = ability.getSequence();
int index = 0;
for(String s : sequence) {
String s1 = properSequence[index];
if(!s1.equalsIgnoreCase(s)) {
matchesSequence = false;
break;
}
index++;
}
if(matchesSequence) {
// Matches, code
}
I was wondering if there's an easier/prettier way of doing this, seems a bit redundant.
答案1
得分: 1
Your code may throw ArrayIndexOutOfBoundsException
. Check the bounds of the array as well as the list as shown below:
for (int i = 0; i < Math.min(sequence.size(), properSequence.length); i++) {
if (!sequence.get(i).equalsIgnoreCase(properSequence[i])) {
matchesSequence = false;
break;
}
}
或者
for (int i = 0; i < sequence.size() && i < properSequence.length; i++) {
if (!sequence.get(i).equalsIgnoreCase(properSequence[i])) {
matchesSequence = false;
break;
}
}
英文:
Your code may throw ArrayIndexOutOfBoundsException
. Check the bounds of array as well as the list as shown below:
for(int i = 0; i < Math.min(sequence.size(), properSequence.length); i++) {
if(!sequence.get(i).equalsIgnoreCase(properSequence[i])) {
matchesSequence = false;
break;
}
}
Alternatively,
for(int i = 0; i < sequence.size() && i < properSequence.length; i++) {
if(!sequence.get(i).equalsIgnoreCase(properSequence[i])) {
matchesSequence = false;
break;
}
}
答案2
得分: 1
你可以使用内置的equals方法:
if (!Arrays.equals(sequence.toArray(), properSequence))
matchesSequence = false;
英文:
You can use built-in equals method:
if(!Arrays.equals(sequence.toArray(),properSequence))
matchesSequence = false;
答案3
得分: 1
你可以将 String[] 转换为 ArrayList,然后进行比较,但如果你想在比较中使用 equalsIgnoreCase(),那么就无法这样做。然而,如果你将所有元素放入 String[] 和 ArrayList 中的大写(或小写)形式,你可以这样做:
if (Arrays.asList(properSequence).equals(sequence))
{
...
}
英文:
You could convert the String[] into an ArrayList and compare those two, but you can't do that if you want equalsIgnoreCase() in the comparison. However, if you put all the elements in the String[] and the ArrayList in upper (or lower) case, you could do this:
if (Arrays.asList(properSequence).equals(sequence))
{
...
}
答案4
得分: 1
Arrays和Lists只有在元素相等且顺序相同的情况下才被视为相等。
要比较一个对象数组(Array of object)与相同对象类型的列表(List),可以使用以下方法,使用Arrays#compare方法。不同版本有不同的功能。此示例使用Strings
并执行不区分大小写的比较。还利用了JDK 11中引入的List.toArray
方法。
List<String> list = List.of("A", "B", "C");
String[] array1 = { "a", "b", "c" };
String[] array2 = { "a", "c", "b" };
for (String[] array : new String[][] { array1, array2 }) {
if (Arrays.compare(array, list.toArray(String[]::new),
String.CASE_INSENSITIVE_ORDER) == 0) {
System.out.printf("%s equals %s%n",
Arrays.toString(array), list);
} else {
System.out.printf("%s does not equal %s%n",
Arrays.toString(array), list);
}
}
打印结果:
[a, b, c] equals [A, B, C]
[a, c, b] does not equal [A, B, C]
英文:
Arrays and Lists are only considered equal to each other if the elements are equal and in the same order.
To compare an Array of object to a List of the same object you can do the following using the Arrays#compare method. Different versions have different capabilities. This example uses Strings
and does a case insensitive compare. It also makes use of List.toArray
introduced in JDK 11
List<String> list = List.of("A", "B", "C");
String[] array1 = { "a", "b", "c" };
String[] array2 = { "a", "c", "b" };
for (String[] array : new String[][] { array1, array2 }) {
if (Arrays.compare(array, list.toArray(String[]::new),
String.CASE_INSENSITIVE_ORDER) == 0) {
System.out.printf("%s equals %s%n",
Arrays.toString(array), list);
} else {
System.out.printf("%s does not equal %s%n",
Arrays.toString(array), list);
}
}
Prints
[a, b, c] equals [A, B, C]
[a, c, b] does not equal [A, B, C]
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论