找出数组类型以在java中比较数组

发布于 2024-12-09 07:26:13 字数 569 浏览 0 评论 0原文

我正在尝试编写一种方法来比较 Java 中的两个数组,而不使用 .equals()。

比较数组的大小/长度后,我想对它们进行排序(假设它们大小相同),但我不知道每个数组中存储的数据类型。我怎样才能弄清楚执行 .sort() ?

我突然想到,我可能可以执行 deepToString() 而不必担心实际类型,因为我只是在这里比较数组。

if(array1.length = array2.length)
{
    //figure out array type to perform sorts???
    array1.sort(TYPE[]);
    array2.sort(TYPE[]);
    for(int i = 0; i < array1.size(); i++)
    {
        if(array1[i] != array2[i])
        {
            return false;
        }
        if(i = array1.size() - 1)
        {
            return true;
        }
    }
}

I'm trying to write a method to compare two arrays in Java without using .equals().

After comparing the size/length of the arrays, I want to sort them (assuming they are the same size), but I don't know what data type is being stored in each array. How can I figure this out to perform a .sort()?

It's occurring to me that I can possibly do a deepToString() and not worry about actual type since I'm only comparing the arrays here.

if(array1.length = array2.length)
{
    //figure out array type to perform sorts???
    array1.sort(TYPE[]);
    array2.sort(TYPE[]);
    for(int i = 0; i < array1.size(); i++)
    {
        if(array1[i] != array2[i])
        {
            return false;
        }
        if(i = array1.size() - 1)
        {
            return true;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为看清所以看轻 2024-12-16 07:26:13

我不知道每个数组中存储的数据类型是什么。

这是一个难题:

  • 您可以获取数组第一个元素的类型并查看其类型;例如

    类类型 = array[0].getClass();

    但是不能保证数组的所有元素都具有相同的(最派生的)类型。

  • 你可以获得数组的基类型;例如

    类类型 = array.getClass().getComponentType();

    但是元素的实际类型可以是该类型的任何子类型。

如何计算出执行 .sort() 的方法?

即使您设法获取元素的实际类型(并且它们都具有相同的类型),您仍然陷入困境。仅知道该类型(通常)不允许您对它们进行排序。要对数组进行排序,您需要满足以下条件之一:

  • 元素类型必须实现 Comparable 接口。

  • 您必须提供单独的 Comparator 对象。

我认为最好的选择是忘记类型并执行以下操作之一:

  • 只需调用 Arrays.sort(Object[])。如果数组元素全部实现Comparable并且元素可以相互比较,则此方法有效。

  • 实现并使用比较器类。理论上,这可能是一个“通用”比较器,可以对数组中可能遇到的任何对象对进行排序。但更好的想法是实现多个比较器,并将适当的比较器作为参数或其他内容传递给您的相等方法。

I don't know what data type is being stored in each array.

That's a difficult question:

  • You could get the type of the first element of the array and look at its type; e.g.

    Class type = array[0].getClass();

    But there's no guarantee that all elements of the array will have the same (most derived) type.

  • You could get the basetype of the array; e.g.

    Class type = array.getClass().getComponentType();

    But the actual type of the elements can be any subtype of that type.

How can I figure this out to perform a .sort()?

Even if you manage to get the actual type of the elements (and they all have the same type), you are still stuck. Just knowing that type won't (in general) allow you to sort them. To sort the array you need one of the following:

  • The element type must implement the Comparable interface.

  • You must supply a separate Comparator object.

I think that your best bet is to forget about the types and do one of the following:

  • Simply call Arrays.sort(Object[]). This works if the array elements all implement Comparable and the elements are mutually comparable.

  • Implement and use a comparator class. In theory, this could be a "universal" comparator that can order any pair of objects that you are likely to encounter in the array. But a better idea would be to implement multiple comparators and pass the appropriate one to your equality method as a parameter or something.

鸠魁 2024-12-16 07:26:13
Class type1 = array1[0].getClass();
Class type2 = array2[0].getClass();
if(type1!=type2)
    return false;

但是你不使用 equals() 的方法是不好的。在不知道数组中的对象类型的情况下,使用 ==(或 !=)运算符可能只是比较对象的地址(除非它们是原始类型)。

运行你的函数

array1 = {"a", "b", "c"};
array2 = {"a", "b", "c"};

将返回 false。

Class type1 = array1[0].getClass();
Class type2 = array2[0].getClass();
if(type1!=type2)
    return false;

But your approach of not using equals() is bad. Without knowing what kind of objects are in the array, using the == (or !=) operator is probably only comparing the addresses of the objects (unless they are primitive types).

Running your function on

array1 = {"a", "b", "c"};
array2 = {"a", "b", "c"};

would return false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文