Java解决No enclosing instance of type PrintListFromTailToHead is accessible问题的两种方案
作者:凌风1205 时间:2022-10-30 00:31:51
今天在编译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问题的两种方案,希望对大家有所帮助。
来源:http://www.cnblogs.com/lfeng1205/archive/2016/07/24/5700735.html
标签:java,enclosing,instance
0
投稿
猜你喜欢
springboot中使用@Transactional注解事物不生效的坑
2021-10-03 10:01:47
Java贪吃蛇游戏完善版
2023-04-12 03:07:53
Android中CountDownTimer 实现倒计时功能
2022-01-03 00:00:24
mybatis-plus与JPA混合的使用方式
2022-06-24 02:10:25
android计算器代码示例分享
2023-10-14 14:06:58
详解Java回环屏障CyclicBarrier
2022-08-30 15:02:09
javaSE基础如何通俗的理解javaBean是什么
2023-09-20 00:34:13
Android Support Palette使用详解
2023-04-05 15:47:24
java 中HashCode重复的可能性
2021-09-13 17:38:05
常用Eclipse快捷方式(推荐)
2022-01-22 04:37:56
java中如何获取时间戳的方法实例
2021-06-14 15:29:16
基于Java代码实现支付充值的通用流程
2021-09-04 00:48:45
详解Android App中创建ViewPager组件的方法
2023-07-12 00:46:14
C#警惕匿名方法造成的变量共享实例分析
2021-08-26 19:35:22
Android动态添加碎片代码实例
2023-10-19 23:35:13
基于运算符重载的那些事
2023-03-29 01:50:51
使用idea解决maven依赖冲突的问题
2021-10-16 12:32:51
java_object的简单使用详解
2023-08-22 11:35:57
js+java实现登录滑动图片验证
2022-02-21 21:17:17
Android基于AlarmManager实现用户在线心跳功能示例
2021-12-02 20:26:46