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 }

 

转载于:https://www.cnblogs.com/lfeng1205/p/5700735.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值