Java 从另一个类访问特定的类变量

Java 从另一个类访问特定的类变量,java,Java,因此,我有一个自定义linkedList(单个)类,它使用提供的节点类 现在我有了另一个类来快速排序(为了简洁起见,我只想专注于编辑L.head) 但是,我不想将快速排序方法传递给整个linkedList,我只想传递linkedList的头 我的问题是访问这个head节点并从quckSort方法/类更改它,我不想删除它或只是更改元素值 主要内容: 公共类TestLinkedList{ 公共静态void main(字符串[]args){ MyLinkedList L=新的MyLinkedList(

因此,我有一个自定义linkedList(单个)类,它使用提供的节点类

现在我有了另一个类来快速排序(为了简洁起见,我只想专注于编辑L.head)

但是,我不想将快速排序方法传递给整个linkedList,我只想传递linkedList的头

我的问题是访问这个head节点并从quckSort方法/类更改它,我不想删除它或只是更改元素值

主要内容:

公共类TestLinkedList{
公共静态void main(字符串[]args){
MyLinkedList L=新的MyLinkedList();
L.加入(3);
L.加入第(1)款;
L.加入第(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;
    }
公共类MySort{
公共静态void快速排序(MyNode列表){
list=list.next;
}
节点类:

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

    public MyNode(E item) {
        element = item;
        next = null;
    }
}
公共类MyNode{
E元素;
MyNode-next;
公共MyNode(E项){
元素=项目;
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 + "\"";
    }
}
公共类MyLinkedList{
公共MyNode头;
/**
*默认构造函数
*/
公共MyLinkedList(){
this.head=新的MyNode(null);
}
/**
*将给定元素添加到MylinkedList
* 
*@param e
*要添加的给定元素
*/
公共空间添加(E){
断言e!=null:“违反:e不为null”;
MyNode temp=新的MyNode(e);
if(head.element!=null){
下一个温度=压头;
压头=温度;
}否则{
这个头=温度;
}
}
/**
*给定某个元素,将尝试查找并返回它是否存在
*在MylinkedList中
* 
*@param e
*正在搜索的元素
*@返回此MyLinkedList是否包含给定
*元素
*/
公共布尔查找(E){
断言e!=null:“违反:e不为null”;
布尔结果=假;
MyNode温度=头部温度;
outerloop:while(temp.element!=null&&temp.next!=null){
如果(温度元素等于(e)){
结果=真;
断开外环;
}
温度=下一个温度;
}
返回结果;
}
/**
*在特定元素之前插入给定元素
* 
*@param e
*之前插入的元素
*@param e2
*正在插入的元素
*/
公共void insertElementBefore(e1、e2){
断言e1!=null:“违反:e1不为null”;
断言e2!=null:“违反:e2不为null”;
MyNode温度=头部温度;
MyNode prev=头部;
MyNode newElement=新的MyNode(e2);
outerloop:while(temp.element!=null&&temp.next!=null){
if(温度元素等于(e1)){
prev.next=新元素;
newElement.next=temp;
断开外环;
}
prev=温度;
温度=下一个温度;
}
}
/**
*从MylinkedList中删除给定元素
* 
*@param e
*正在删除的元素
*/
公共作废删除(E){
断言e!=null:“违反:e不为null”;
MyNode温度=头部温度;
MyNode prev=头部;
循环:while(temp.element!=null){
如果(温度元素等于(e)){
如果(温度==水头){
压头=下一个温度;
}否则{
上一个=下一个温度;
}
断环;
}
prev=温度;
温度=下一个温度;
}
}
/**
*重写为字符串;干净地打印所有元素
*/
公共字符串toString(){
字符串结果=“\”;
MyNode温度=头部温度;
循环:while(temp.element!=null){
结果+=临时元素+“”;
如果(临时下一步!=null){
温度=下一个温度;
}否则{
断环;
}
}
返回结果+“\”;
}
}

在java中,参数是按值传递的。也就是说,您不能通过在本地方法中更改引用的地址来修改引用的地址。我建议您做的是让QuickSort()方法返回下一个节点地址

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 + "\"";
    }
}