【问题标题】:Java: How to refer to a class inside the class but of another element?Java:如何在类内部引用另一个元素的类?
【发布时间】:2011-04-13 01:01:13
【问题描述】:

我不确定如何解释这一点,但基本上我想参考 Listfront 这是 Element A(可以来自任何列表)。但是发生的情况是,当它通过列表的元素时,它正在比较两个不同的列表并最终不匹配。即比较包含前面 b 的原始列表与包含元素 A 的列表。现在我只是想知道如何将 元素 A 的前面设置为 b 以便我可以比较它的位置。

/*front is a dummy element used to keep position.
List is a class i have made under requirements of naming for subject.
i don't want a solution. I only want to know about how to do it.

This is what is an example code of whats causing the problem USED IN DRIVER PROGRAM
DLL.concat(DLL2);
it is basically getting DLL's front and going through the loop when it should be using DLL2's.

DLL and DLL2 are both Lists
***/


    //will return the index of the Element for comparing

    private int checkElement(Element A){

        Element b = front;

            int i = 0;  
            while (b != a && i<size)
            {
                b = b.next;
                i++;
            }

            return i;
        }


//edit: add

//size is the size of the list gets increased everytime a variable is added to the list on top of the dummy element.

//Item is a private class inside the List class. it contains the values: element,next, previous in which element contains an object, next and previous contain the next element in the list and the previous one (its a double linked list) 

// this is what causes the error to turn up in the above method as im using two different lists and joining them.

    public void concat(List L){
        if  (splice(L.first(),L.last(),last())){
            size = size+L.size;
        }
    }

//this is the splice method for cutting out elements and attaching them after t
//just using the check method to assert that a<b and will later use it to assert t not inbetween a and b

public boolean splice(Element a, Element b, Element t){

        if  (checkElement(a) < checkElement(b)){

            Element A = a.previous;
            Element B = b.next;
            A.next = B;
            B.previous = A;

            Element T = t.next;

            b.next = T;
            a.previous = t;
            t.next = a;
            T.previous = b;
        return true;
        }
        else {

        System.out.println("Splicing did not occur due to b<a");        
        return false;
        }

    }

【问题讨论】:

  • 对不起,我认为语言障碍让一个复杂的问题变得更加复杂。你能告诉我们更多的代码吗?比如size是什么,front是什么?此代码是列表结构的内部代码吗?

标签: java class methods this refer


【解决方案1】:

因此,尽管我发表了评论,但我还是看到了一个明显的问题。您不能在引用类型上使用相等运算符。也就是说,除了原始类型(double、int 等)之外的任何东西。发生的情况是您正在比较实例的地址,除非它们实际上是相同的对象(内存中的相同地址),否则它永远不会返回 true。也许这就是你想要的,但我怀疑不是。你需要重写方法

public boolean equals(Object obj);

并使用它来比较给定类的两个实例。我的假设是否正确?

编辑好的,我认为我最初的猜测是正确的。如果它们来自同一个列表,它会起作用,因为它们最终是相同的元素(存储在相同的内存位置)。您需要使用equals()!equals() 而不是==!=。试试看,看看它是否能解决你的问题。此外,不要只使用它们,您必须覆盖 equals 才能实际比较元素的内部属性。

【讨论】:

  • 当我使用列表中的元素时,它确实有效。但是,如果我使用另一个列表中的元素,它仍然引用原始列表...我会发布更多代码
  • 如在上面的编辑中,如果我使用一种仅在一个列表上使用拼接方法的方法,它的工作原理是在 checkelement 方法中创建的元素 b 来自同一个列表。这就是为什么我需要知道是否有一种方法,比如使用 this 来引用 Item a 的前面(这是一个虚拟值),因此能够让它工作。
  • 我知道 equals 和其他东西,但这不是我所追求的。我基本上只是想知道如何能够将元素 b 设置为包含元素 A 的列表的 front 即。当我希望 List1.concat(List2) 成为 List2 的时,List1.concat(List2) 会将集合元素 b 设置为 List1 的前面
猜你喜欢
  • 2010-09-23
  • 1970-01-01
  • 2012-08-17
  • 1970-01-01
  • 2017-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
相关资源
最近更新 更多