簡體   English   中英

從另一個類訪問特定的類變量

[英]Accessing specific class variables from another class

因此,我有一個使用提供的節點類的自定義linkedList(single)類。

現在我有另一個類對此進行QuickSort。(為簡潔起見,我只想專注於編輯L.head)

但是,我沒有傳遞整個鏈表的quicksort方法,而是傳遞鏈表的頭。

我的問題是訪問此頭節點並從quckSort方法/類中對其進行更改,而我不想刪除它或只是更改元素值

主要:

public class TestLinkedList {
    public static <E extends Comparable<E>> void main(String[] args) {
        MyLinkedList<Integer> L = new MyLinkedList<Integer>();
        L.add(3);
        L.add(1);
        L.add(2);
        System.out.println("Initial=" + L);
        MySort.quickSort(L.head);
        System.out.println("After  ="+L);
    }
}

快速排序:

public class MySort {
    public static <E extends Comparable<E>> void quickSort(MyNode<E> list) {

        list = list.next;
    }

節點類別:

public class MyNode<E extends Comparable<E>> {
    E element;
    MyNode<E> next;

    public MyNode(E item) {
        element = item;
        next = null;
    }
}

LinkedList類:

public class MyLinkedList<E extends Comparable<E>> {

    public MyNode<E> head;

    /**
     * Default constructor
     */
    public MyLinkedList() {
        this.head = new MyNode<E>(null);
    }

    /**
     * Adds given element to MylinkedList
     * 
     * @param e
     *            Given element to be added
     */
    public void add(E e) {
        assert e != null : "Violation of: e is not null";

        MyNode<E> temp = new MyNode<E>(e);

        if (head.element != null) {
            temp.next = head;
            head = temp;
        } else {
            this.head = temp;
        }
    }

    /**
     * Given a certain element, will try to locate and return whether it exists
     * in the MylinkedList
     * 
     * @param e
     *            Element being searched for
     * @return Result Result of whether this MyLinkedList contains the given
     *         Element
     */
    public boolean find(E e) {
        assert e != null : "Violation of: e is not null";

        boolean result = false;
        MyNode<E> temp = head;

        outerloop: while (temp.element != null && temp.next != null) {
            if (temp.element.equals(e)) {
                result = true;
                break outerloop;
            }
            temp = temp.next;
        }
        return result;
    }

    /**
     * Inserts given element before specific element
     * 
     * @param e
     *            Element being inserted before
     * @param e2
     *            Element being inserted
     */
    public void insertElementBefore(E e1, E e2) {
        assert e1 != null : "Violation of: e1 is not null";
        assert e2 != null : "Violation of: e2 is not null";
        MyNode<E> temp = head;
        MyNode<E> prev = head;
        MyNode<E> newElement = new MyNode<E>(e2);

        outerloop: while (temp.element != null && temp.next != null) {
            if (temp.element.equals(e1)) {
                prev.next = newElement;
                newElement.next = temp;
                break outerloop;
            }
            prev = temp;
            temp = temp.next;
        }
    }

    /**
     * Deletes given element from MylinkedList
     * 
     * @param e
     *            Element being deleted
     */
    public void delete(E e) {
        assert e != null : "Violation of: e is not null";

        MyNode<E> temp = head;
        MyNode<E> prev = head;

        loop: while (temp.element != null) {
            if (temp.element.equals(e)) {
                if (temp == head) {
                    head = temp.next;
                } else {
                    prev.next = temp.next;
                }
                break loop;
            }
            prev = temp;
            temp = temp.next;
        }
    }

    /**
     * Overridden toString; prints out all elements cleanly
     */
    public String toString() {
        String result = "\"";
        MyNode<E> temp = head;

        loop: while (temp.element != null) {
            result += temp.element + " ";
            if (temp.next != null) {
                temp = temp.next;
            } else {
                break loop;
            }
        }
        return result + "\"";
    }
}

在Java中,參數是通過值傳遞的。 也就是說,您不能通過在本地方法中更改引用的地址來對其進行修改。 我建議您做的是使QuickSort()方法返回下一個Node地址。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM