比较两个字符串数组而不索引每个元素的最佳方法是什么? [英] What is the best way to compare two string arrays without indexing through each element?

查看:60
本文介绍了比较两个字符串数组而不索引每个元素的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种有效的方法可以用来比较两个字符串数组而无需检查每个元素?

Is there any efficient way that can be used to compare two string arrays without checking through each element?

我尝试使用数组列表来构建对象,但是没有一个显示出很大的效果.

I tried using Array Lists, building objects, but none of them didn't show much effect.

推荐答案

最后,要比较两个字符串的序列"(代表字符序列),没有效率的魔术.

In the end, to compare two "sequences" of Strings (which represent sequences of characters), there is no magic trick for efficiency.

您必须以某种方式浏览这两个序列,然后逐个元素进行比较.

You have to somehow walk through both sequences and compare them element by element.

从这个意义上讲,您唯一可以争取的就是:高效"的编码风格;换句话说:专注于编写人类可读的代码.

In that sense, the only thing you can strive for is: "efficient" coding style; in other words: focus on writing human readable code.

然后,当然,现有的库调用Arrays.equals()将是您的第一站.除此之外:您可以确定自己写下来的任何内容都不会比标准内置解决方案更强".

And then of course, the existing library call Arrays.equals() would be your first stop. Beyond that: you can be pretty sure that whatever you can write down yourself will not perform "better" than the standard built-in solution.

除此之外:请记住,在决定:两个相等大小的数组是否具有相同的元素之间存在区别.还是它们的元素具有完全相同的顺序?

Beyond that: keep in mind that there is a difference between deciding: do two equally sized arrays have the same elements; or do they have the exact same order of elements on top of that?

换句话说;必须澄清[A,B]是否等于[B,A]-决定是否

In other words; you have to clarify if [A, B] is equal to [B, A] - to decide if Array.equals() with

如果两个指定的Object数组彼此相等,则返回true.如果两个数组包含相同数量的元素,并且两个数组中所有对应的元素对都相等,则认为这两个数组相等.如果两个对象e1和e2相等,则认为(e1 == null?e2 == null:e1.equals(e2)).换句话说,如果两个数组包含相同顺序的相同元素,则它们相等.另外,如果两个数组引用都为空,则它们被认为是相等的.

Returns true if the two specified arrays of Objects are equal to one another. The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. Two objects e1 and e2 are considered equal if (e1==null ? e2==null : e1.equals(e2)). In other words, the two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.

确实是您所需要的!

如果顺序对您来说不重要,那么您首先必须对两个数组进行排序-因为这会将两个数组内容置于相同的顺序.

If the order is not important to you, you would first have to sort both arrays - as that puts both array content into the same order.

最后的想法:从性能角度来看,在这里使用数组和ArrayList之间应该没有(太多)区别.在我的回答上下文中,我们只是发现通过放下 listA.equals(listB)(这将为您提供Arrays.equals()语义),您可以更轻松地与列表进行比较.

Final thought: performance wise, there shouldn't be (much) difference between using arrays and ArrayList here. And in the context of my answer, we simply find that you can compare to lists even more easily by putting down listA.equals(listB) (which will give you the Arrays.equals() semantics).

可能真正影响性能的唯一方面是:用例何时允许使用集...

The only aspect that might really affect performance is: when your use case would allow for using sets ...

这篇关于比较两个字符串数组而不索引每个元素的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆