星空网 > 软件开发 > Java

Java解决No enclosing instance of type PrintListFromTailToHead is accessible问题

今天在编译Java程序时遇到如下问题:

No enclosing instance of type PrintListFromTailToHead is accessible. Must qualify the allocation with an enclosing instance
of type PrintListFromTailToHead (e.g. x.new A() where x is an instance of PrintListFromTailToHead).

源代码为:

 

 1 public class PrintListFromTailToHead { 2  3   public static void main(String[] args) { 4     ListNode one = new ListNode(1); 5     ListNode two = new ListNode(2); 6     ListNode three = new ListNode(3); 7     one.next = two; 8     two.next = three; 9     10     ArrayList<Integer> result = printListFromTailToHead(one);11 12     System.out.println("结果是:" + result);13   }14   15   class ListNode {16 17     public int val;18     public ListNode next;19 20     public ListNode() {21 22     }23 24     public ListNode(int val) {25       this.val = val;26     }27   }28 29   public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {30 31     Stack<Integer> stack = new Stack<Integer>();32     while (listNode != null) {33       stack.push(listNode.val);34       listNode = listNode.next;35     }36 37     ArrayList<Integer> arrayList = new ArrayList<Integer>();38     while (!stack.isEmpty()) {39       arrayList.add(stack.pop());40     }41     return arrayList;42   }  45 }

 

问题解释:

代码中,我的ListNode类是定义在PrintListFromTailToHead类中的内部类。ListNode内部类是动态的内部类,而我的main方法是static静态的。

就好比静态的方法不能调用动态的方法一样。

有两种解决办法:

第一种:

将内部类ListNode定义成静态static的类。

第二种:

将内部类ListNode在PrintListFromTailToHead类外边定义。

 

两种解决方法:

第一种:

 1 public class PrintListFromTailToHead { 2  3   public static void main(String[] args) { 4     ListNode one = new ListNode(1); 5     ListNode two = new ListNode(2); 6     ListNode three = new ListNode(3); 7     one.next = two; 8     two.next = three; 9     10     ArrayList<Integer> result = printListFromTailToHead(one);11 12     System.out.println("结果是:" + result);13   }14   15   static class ListNode {16 17     public int val;18     public ListNode next;19 20     public ListNode() {21 22     }23 24     public ListNode(int val) {25       this.val = val;26     }27   }

第二种:

 1 public class PrintListFromTailToHead { 2  3   public static void main(String[] args) { 4     ListNode one = new ListNode(1); 5     ListNode two = new ListNode(2); 6     ListNode three = new ListNode(3); 7     one.next = two; 8     two.next = three; 9   }10 11     public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {12 13     Stack<Integer> stack = new Stack<Integer>();14     while (listNode != null) {15       stack.push(listNode.val);16       listNode = listNode.next;17     }18 19     ArrayList<Integer> arrayList = new ArrayList<Integer>();20     while (!stack.isEmpty()) {21       arrayList.add(stack.pop());22     }23     return arrayList;24   }25 }26 27 class ListNode {28 29   public int val;30   public ListNode next;31 32   public ListNode() {33 34   }35 36   public ListNode(int val) {37     this.val = val;38   }39 }

 




原标题:Java解决No enclosing instance of type PrintListFromTailToHead is accessible问题

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

亚马逊卖家的“2017⇆2019”,太南了!:https://www.ikjzd.com/articles/111983
Lazada推出交付保证服务,24小时未收到货将自动补偿!:https://www.ikjzd.com/articles/111984
买账号被骗,注册新号遇二审,2020年亚马逊更难!:https://www.ikjzd.com/articles/111985
非洲电商平台Jumia退出坦桑尼亚市场:https://www.ikjzd.com/articles/111986
掘金越南! Shopee跨境电商不可错过的黄金市场!:https://www.ikjzd.com/articles/111991
圣诞节要变圣诞“劫”?又有产品被亚马逊下架!:https://www.ikjzd.com/articles/111992
重磅利好!欧洲大国暂停100多种商品进口税:https://www.kjdsnews.com/a/1842170.html
申根签证页编号是什么意思 申根签证内容:https://www.vstour.cn/a/409227.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流