如何让一组Java数组的? [英] how to make a set of array in java?

查看:118
本文介绍了如何让一组Java数组的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于在阵列等号功能只检查实例,它不很好地集工作。
因此,我不知道如何让一组阵列在Java中?

Since the equals function in array only check the instance, it doesn't work well with Set. Hence, I wonder how to make a set of arrays in java?

一个可能的方法可以被放在一个对象每个阵列,并实现相当于函数,该函数类,但会过多降低性能?

One possible way could be put each array in an object, and implement equals function for that class, but will that decrease the performance too much?

推荐答案

由于ArrayList类已经封装了数组,你可以扩展它和重写等于散code 方法。这里有一个例子:

Since the ArrayList class already wraps an array, you can extend it and override the equals and hashCode methods. Here is a sample:

public MyArrayList extends ArrayList<MyClass> {

    @Override
    public boolean equals(Object o) {
        if (o instanceof MyArrayList) {
            //place your comparison logic here
            return true;
        }
        return false;
    }

    @Override
    public int hashCode() {
        //just a sample, you can place your own code
        return super.hashCode();
    }
}

更新:

您甚至可以覆盖它一个通用的使用,只是改变了code为:

You can even override it for a generic use, just changing the code to:

public MyArrayList<T> extends ArrayList<T> {
    //overrides the methods you need
    @Override
    public boolean equals(Object o) {
        if (o instanceof MyArrayList) {
            //place your comparison logic here
            return true;
        }
        return false;
    }
}

这篇关于如何让一组Java数组的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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