比较字符串数组与数组列表

huangapple go评论98阅读模式
英文:

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&lt;String&gt; 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 &lt; Math.min(sequence.size(), properSequence.length); i++) {
   if(!sequence.get(i).equalsIgnoreCase(properSequence[i])) {
      matchesSequence = false;
      break;
   }
}

Alternatively,

for(int i = 0; i &lt; sequence.size() &amp;&amp; i &lt; 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&lt;String&gt; list = List.of(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;);
String[] array1 = { &quot;a&quot;, &quot;b&quot;, &quot;c&quot; };
String[] array2 = { &quot;a&quot;, &quot;c&quot;, &quot;b&quot; };

for (String[] array : new String[][] { array1, array2 }) {
	
	if (Arrays.compare(array, list.toArray(String[]::new),
			String.CASE_INSENSITIVE_ORDER) == 0) {
		System.out.printf(&quot;%s equals %s%n&quot;,
				Arrays.toString(array), list);
	} else {
		System.out.printf(&quot;%s does not equal %s%n&quot;,
				Arrays.toString(array), list);
	}
}

Prints

[a, b, c] equals [A, B, C]
[a, c, b] does not equal [A, B, C]

</details>



huangapple
  • 本文由 发表于 2020年8月9日 23:55:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63328425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定