Java 无法从类中的方法返回泛型类型的数组

Java 无法从类中的方法返回泛型类型的数组,java,Java,我正在实现我自己的通用链表类,它有一个名为toArray的实例方法,该方法创建链表的数组副本并返回它。然而,每当我试图在实例上调用该方法时,我总是收到错误消息“SLList.this不能从静态上下文引用”。我搜索了一段时间,有人说这是因为我没有在实例上调用该方法,但我确实调用了 下面是课堂: public class SLList<ElemType>{ private class StuffNode{ public ElemType item;

我正在实现我自己的通用链表类,它有一个名为
toArray
的实例方法,该方法创建链表的数组副本并返回它。然而,每当我试图在实例上调用该方法时,我总是收到错误消息“SLList.this不能从静态上下文引用”。我搜索了一段时间,有人说这是因为我没有在实例上调用该方法,但我确实调用了

下面是课堂:

public class SLList<ElemType>{

    private class StuffNode{
        public ElemType item;
        public StuffNode next;
    
        public StuffNode(ElemType i, StuffNode n){
            item = i;
            next = n;
        }
    }
    /** The first item of a list(if it exists) is at sentinel.next*/
    private StuffNode sentinel;
    private int size;

        public ElemType[] toArray(){
           ElemType[] arr =(ElemType[]) new Object[this.size];
           StuffNode ptr = this.sentinel;
           int i = 0;
           while(ptr.next != null){
               arr[i] = ptr.next.item;
               ptr = ptr.next;
               i++;
        }
           return arr;
    }

}


ElemType[]arr=x.toArray()
是我不断收到错误消息的那一行,我使用了一个在线Java可视化工具来确认该方法工作正常,我只是在返回结果时遇到了问题

您可以这样做:

public class Main2 {
    public static void main(String[] args) {
        SLList<Integer> x = new Main2().new SLList<>(3);
        x.addLast(4);
        x.addFirst(1);
        Integer[] arr = x.toArray();
    }

    public class SLList<ElemType> {
        private class StuffNode {
            public ElemType item;
            public StuffNode next;

            public StuffNode(ElemType i, StuffNode n) {
                item = i;
                next = n;
            }
        }
    
        public SLList(ElemType n) {
            // Some code for constructor
        }
    
        public void addFirst(ElemType n) {
            // Some code
        }

        public void addLast(ElemType n) {
            // Some code
        }

        /**
         * The first item of a list(if it exists) is at sentinel.next
         */
        private StuffNode sentinel;
        private int size;

        public ElemType[] toArray() {
            ElemType[] arr = (ElemType[]) new Object[this.size];
            StuffNode ptr = this.sentinel;
            int i = 0;
            while (ptr.next != null) {
                arr[i] = ptr.next.item;
                ptr = ptr.next;
                i++;
            }
            return arr;
        }
    }
}
public class Main2 {
    public static void main(String[] args) {
        SLList<Integer> x = new SLList<>(3);
        x.addLast(4);
        x.addFirst(1);
        Integer[] arr = x.toArray();
    }

    // INSERT A STATIC HERE
    public static class SLList<ElemType> {
        private class StuffNode {
            public ElemType item;
            public StuffNode next;

            public StuffNode(ElemType i, StuffNode n) {
                item = i;
                next = n;
            }
        }

        public SLList(ElemType n) {
            // Some code for constructor
        }

        public void addFirst(ElemType n) {
            // Some code
        }

        public void addLast(ElemType n) {
            // Some code
        }

        /**
         * The first item of a list(if it exists) is at sentinel.next
         */
        private StuffNode sentinel;
        private int size;

        public ElemType[] toArray() {
            ElemType[] arr = (ElemType[]) new Object[this.size];
            StuffNode ptr = this.sentinel;
            int i = 0;
            while (ptr.next != null) {
                arr[i] = ptr.next.item;
                ptr = ptr.next;
                i++;
            }
            return arr;
        }
    }
}
公共类Main2{
公共静态void main(字符串[]args){
SLList x=new main 2()。新建SLList(3);
x、 addLast(4);
x、 第一(1);
整数[]arr=x.toArray();
}
公共类列表{
私有类填充节点{
公共元素类型项;
下一步是公共信息节点;
公共StuffNode(元素类型i,StuffNode n){
项目=一;
next=n;
}
}
公共SLList(元素类型n){
//构造函数的一些代码
}
公共void addFirst(元素类型n){
//一些代码
}
公共void addLast(元素类型n){
//一些代码
}
/**
*列表的第一项(如果存在)位于sentinel.next
*/
私有节点哨兵;
私有整数大小;
公共元素类型[]toArray(){
ElemType[]arr=(ElemType[])新对象[this.size];
StuffNode ptr=this.sentinel;
int i=0;
while(ptr.next!=null){
arr[i]=ptr.next.item;
ptr=ptr.next;
i++;
}
返回arr;
}
}
}
或者像这样:

public class Main2 {
    public static void main(String[] args) {
        SLList<Integer> x = new Main2().new SLList<>(3);
        x.addLast(4);
        x.addFirst(1);
        Integer[] arr = x.toArray();
    }

    public class SLList<ElemType> {
        private class StuffNode {
            public ElemType item;
            public StuffNode next;

            public StuffNode(ElemType i, StuffNode n) {
                item = i;
                next = n;
            }
        }
    
        public SLList(ElemType n) {
            // Some code for constructor
        }
    
        public void addFirst(ElemType n) {
            // Some code
        }

        public void addLast(ElemType n) {
            // Some code
        }

        /**
         * The first item of a list(if it exists) is at sentinel.next
         */
        private StuffNode sentinel;
        private int size;

        public ElemType[] toArray() {
            ElemType[] arr = (ElemType[]) new Object[this.size];
            StuffNode ptr = this.sentinel;
            int i = 0;
            while (ptr.next != null) {
                arr[i] = ptr.next.item;
                ptr = ptr.next;
                i++;
            }
            return arr;
        }
    }
}
public class Main2 {
    public static void main(String[] args) {
        SLList<Integer> x = new SLList<>(3);
        x.addLast(4);
        x.addFirst(1);
        Integer[] arr = x.toArray();
    }

    // INSERT A STATIC HERE
    public static class SLList<ElemType> {
        private class StuffNode {
            public ElemType item;
            public StuffNode next;

            public StuffNode(ElemType i, StuffNode n) {
                item = i;
                next = n;
            }
        }

        public SLList(ElemType n) {
            // Some code for constructor
        }

        public void addFirst(ElemType n) {
            // Some code
        }

        public void addLast(ElemType n) {
            // Some code
        }

        /**
         * The first item of a list(if it exists) is at sentinel.next
         */
        private StuffNode sentinel;
        private int size;

        public ElemType[] toArray() {
            ElemType[] arr = (ElemType[]) new Object[this.size];
            StuffNode ptr = this.sentinel;
            int i = 0;
            while (ptr.next != null) {
                arr[i] = ptr.next.item;
                ptr = ptr.next;
                i++;
            }
            return arr;
        }
    }
}
公共类Main2{
公共静态void main(字符串[]args){
SLList x=新的SLList(3);
x、 addLast(4);
x、 第一(1);
整数[]arr=x.toArray();
}
//在此处插入一个静态文件
公共静态类列表{
私有类填充节点{
公共元素类型项;
下一步是公共信息节点;
公共StuffNode(元素类型i,StuffNode n){
项目=一;
next=n;
}
}
公共SLList(元素类型n){
//构造函数的一些代码
}
公共void addFirst(元素类型n){
//一些代码
}
公共void addLast(元素类型n){
//一些代码
}
/**
*列表的第一项(如果存在)位于sentinel.next
*/
私有节点哨兵;
私有整数大小;
公共元素类型[]toArray(){
ElemType[]arr=(ElemType[])新对象[this.size];
StuffNode ptr=this.sentinel;
int i=0;
while(ptr.next!=null){
arr[i]=ptr.next.item;
ptr=ptr.next;
i++;
}
返回arr;
}
}
}

将其设置为公共静态类您可能在主类中创建了SLList。如果你想从静态上下文中使用它,你需要把它变成一个静态类:“publicstaticclasssllist”,或者你可以这样做:“newmain().newsllist(3);”