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).
源代码为:
public class PrintListFromTailToHead { public static void main(String[] args) { ListNode one = new ListNode(1); ListNode two = new ListNode(2); ListNode three = new ListNode(3); one.next = two; two.next = three; ArrayList<Integer> result = printListFromTailToHead(one); System.out.println("结果是:" + result); } class ListNode { public int val; public ListNode next; public ListNode() { } public ListNode(int val) { this.val = val; } } public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) { Stack<Integer> stack = new Stack<Integer>(); while (listNode != null) { stack.push(listNode.val); listNode = listNode.next; } ArrayList<Integer> arrayList = new ArrayList<Integer>(); while (!stack.isEmpty()) { arrayList.add(stack.pop()); } return arrayList; } }
问题解释:
代码中,我的ListNode类是定义在PrintListFromTailToHead类中的内部类。ListNode内部类是动态的内部类,而我的main方法是static静态的。
就好比静态的方法不能调用动态的方法一样。
有两种解决办法:
第一种:
将内部类ListNode定义成静态static的类。
第二种:
将内部类ListNode在PrintListFromTailToHead类外边定义。
两种解决方法:
第一种:
public class PrintListFromTailToHead { public static void main(String[] args) { ListNode one = new ListNode(1); ListNode two = new ListNode(2); ListNode three = new ListNode(3); one.next = two; two.next = three; ArrayList<Integer> result = printListFromTailToHead(one); System.out.println("结果是:" + result); } static class ListNode { public int val; public ListNode next; public ListNode() { } public ListNode(int val) { this.val = val; } }
第二种:
public class PrintListFromTailToHead { public static void main(String[] args) { ListNode one = new ListNode(1); ListNode two = new ListNode(2); ListNode three = new ListNode(3); one.next = two; two.next = three; } public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) { Stack<Integer> stack = new Stack<Integer>(); while (listNode != null) { stack.push(listNode.val); listNode = listNode.next; } ArrayList<Integer> arrayList = new ArrayList<Integer>(); while (!stack.isEmpty()) { arrayList.add(stack.pop()); } return arrayList; } } class ListNode { public int val; public ListNode next; public ListNode() { } public ListNode(int val) { this.val = val; } }
以上所述是小编给大家介绍的Java解决No enclosing instance of type PrintListFromTailToHead is accessible问题的两种方案,希望对大家有所帮助。
- JAVA8 List<List<Integer>> list中再装一个list转成一个list操作
- Java ArrayList add(int index, E element)和set(int index, E element)两个方法的说明
- Java servlet、filter、listener、interceptor之间的区别和联系
- JAVA中list,set,数组之间的转换详解
- Java 数组转List的四种方式小结
- Java ArrayList 数组之间相互转换
- java中数组list map三者之间的互转介绍
- java list与数组之间的转换详细解析
- Java编程实现数组转成list及list转数组的方法
- 浅谈java 字符串,字符数组,list间的转化
- java list,set,map,数组间的相互转换详解
- Java中将 int[] 数组 转换为 List分享
相关文章
java封装空值建议使用Optional替代null的方法示例解析
这篇文章主要为大家介绍了java封装空值建议使用Optional替代null的方法原理解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-11-11java中httpclient封装post请求和get的请求实例
这篇文章主要介绍了java中httpclient封装post请求和get的请求实例,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-10-10
最新评论