Java解决No enclosing instance of type PrintListFromTailToHead is accessible问题
2016-07-24 14:20
661 查看
今天在编译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).
源代码为:
问题解释:
代码中,我的ListNode类是定义在PrintListFromTailToHead类中的内部类。ListNode内部类是动态的内部类,而我的main方法是static静态的。
就好比静态的方法不能调用动态的方法一样。
有两种解决办法:
第一种:
将内部类ListNode定义成静态static的类。
第二种:
将内部类ListNode在PrintListFromTailToHead类外边定义。
两种解决方法:
第一种:
第二种:
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中split方法对结尾空字符串不正确处理的解决方案
- Java 多线程中。两个线程交替执行,一个输出偶数,一个输出奇数
- SpringBoot学习笔记(4) Spring Boot 集成 Mybatis
- Java 反射
- JavaWeb之Servlet
- 二叉树的循环遍历方法,Java实现。利用栈和arraylist
- 精选30道Java笔试题解答
- JavaSE基础(六)
- 链表分析笔记
- javaIO流总结
- 性能分析系列——小命令保证大性能
- Spring 框架的设计理念与设计模式分析
- Java获取客户端IP地址-转
- SpringBoot学习笔记(3) Spring Boot 运行原理,自动配置
- Spring 框架简介
- Spring面试题
- Java通过反射机制动态设置对象属性值的方法
- Java 项目的命名规范
- java包装类及自动拆装箱和自动拆箱
- Java的反射机制---动态调用对象的简单方法