在Java中堆叠时,“包含”问题就会出现。 [英] Stack in Java, problem with "contains"

查看:81
本文介绍了在Java中堆叠时,“包含”问题就会出现。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在程序中使用堆栈,但是当程序尝试检查堆栈中包含哪些元素时出现问题。我在Stack中使用整数数组。简短示例为:

I'm using stack in my program but I have a problem when program is trying to check what element contains in stack. I'm using array of integer in Stack. Short example is:

        Stack<int[]> myStack = new Stack<int[]>();
        myStack.push(new int[]{1,2});
        myStack.push(new int[]{1,3});
        myStack.push(new int[]{1,4});
        if (myStack.contains(new int[]{1,3})) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }

现在将其打印为否。我该怎么做才能获得是?
我知道问题是我没有使用相同的对象,但实际上我的程序更大,并且我无法使用它

Now it was printed "NO". How could I do to get "YES" ? I know that the problem is that I'm not using same object but in the reality my program is much larger and I can't to use this

int[] myInteger = new int[]{1,3};
myStack.push(myInteger);
myStack.contains(myInteger);


推荐答案

int []数组对象无法知道如果两个数组相等,因为它们实际上只是指针,并且由于它们指向两个不同的数组,则认为它们不相等。使用指针比较相等性的原因是因为Object类以这种方式定义了最基本的equals方法。

the int[] array objects have no way of knowing if two arrays are equal because they are in effect just pointers, and since they are pointing to two different arrays, they are considered unequal. The reason why the pointers are used to compare equality is because the Object class defines the most rudimentary equals method in such a fashion.

您需要创建一个简单的类来封装您的数组。
这是任何人都可以理解的简单内容。

You need to create a simple class to encapsulate your array. Here is a simple one anyone could understand.

class IntArray {
    int[] myArray;
    public IntArray () {
        myArray = new int[0];
    }
    public IntArray (int [] array) {
        myArray = array;
    }
    /**
     * This method is for accessing the array.
     * The array CAN be modified using this method.
     */
    public int[] getArray() {
        return myArray;
    }
    /**
     * Uses built-in hashCode generating method
     * within the Arrays class.
     * Importing java.util.Arrays is necessary.
     */
    public int hashCode() {
        return Arrays.hashCode(myArray);
    }
    public boolean equals(Object o) {
        if (!(o instanceof IntArray))
            return false;

        //Should use Arrays.equals(o.myArray, myArray);

        if (o.myArray.length != myArray.length)
            return false;
        else {
            for (int i = 0; i < myArray.length; i++) {
                if (myArray[i] != o.myArray[i]) {
                    return false;
                }
            }
            return true;
        }
    }
}

完成此操作后,您

    Stack<In> myStack = new Stack<int[]>();
    myStack.push( new IntArray (new int[]{1,2}) );
    myStack.push( new IntArray (new int[]{1,3}) );
    myStack.push( new IntArray (new int[]{1,4}) );
    if (myStack.contains( new IntArray (new int[]{1,3})) ) {
        System.out.println("YES");
    } else {
        System.out.println("NO");
    }

那肯定可以。

这篇关于在Java中堆叠时,“包含”问题就会出现。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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