Java解决No enclosing instance of type PrintListFromTailToHead is accessible问题的两种方案
作者:凌风1205 发布时间:2022-10-30 00:31:51
标签:java,enclosing,instance
今天在编译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


猜你喜欢
- 1.抽象类与抽象方法:(1)使用关键字abstract修饰的类,称为抽象类.(2)抽象类只是用到一个类所具有的行为,不能单独通过创建对象来使
- 在项目开发中经常会遇到调用第三方接口的情况,比如说调用第三方的天气预报接口。使用流程【1】准备工作:在项目的工具包下导入HttpClient
- 1.简单介绍一下NDK和JNINDK:NDK是Native Development Kit的缩写,是Google提供的一套工具集
- 1. 确保你项目能编译通过,安装java jdk 环境填写环境变量2. 添加SpringBootServletInitializer的子类重
- 上一篇文章我们介绍了java实现的各种排序算法代码示例,本文我们看看Java对象的xml序列化与反序列化的相关内容,具体如下。XML是一种标
- spring是目前最流行的框架。创建java web项目时,我们首先会遇到的配置文件就是web.xml,这是javaweb为我们封装的逻辑,
- 今天给大家分享纯注解版spring与mybatis的整合mybatis包下:有这几个,上面图片没有展开配置Bean:MyBatisAutoC
- java 中 System.out.println()和System.out.write()的区别.这两个函数一个是System
- 目录前言1.设计模式:单例模式1.1 使用时分配,1.2 声明时实例化1.3 双检锁1.4 .net 特性保证的线程安全1.5 使用DI依赖
- 环境:主机:WIN10开发环境:Android Studio 2.2 Preview 3说明:两种方法实现底部弹出的对话框:DialogDi
- 题目题目要求思路:模拟用一个哈希表记录可出现的字母,然后逐一遍历每个单词每个字母,符合条件则结果加一。Javaclass Solution
- 环境:SpringFramework:4.3.5.RELEASEapollo-client:1.5.11.在项目的 resources/ME
- 一、Shiro简介:Apache Shiro是一个Java的安全(权限)框架。Shiro 可以非常容易的开发出足够好的应用,其不仅可以用在J
- 模板消息文档公众号的类型分为服务号、订阅号和企业号,其中服务号和订阅号比较常见。要想实现公众号推动消息给指定的用户,其类型必须为服务号。推送
- 1. 多行编辑先来体验一下从xml文件拷贝字段新建实体对象一般我们为了新建多表连接后映射的 ResultMap ,耗费不少时间,那么我们就来
- 判断有无虚拟按键(导航栏)现在很大一部分手机没有虚拟按键,一部分有。我们在做适配的时候可能会用到这方面的知识。例如:屏幕填充整个屏幕的时候,
- 前述园子里有许多人对log4net这款开源的日志记录控件有很多介绍。在这里个人再做一次总结,希望对以后有所帮助,需要的时候可以直接使用,减少
- 性能优化点:1.使用int不使用double。(单位用分不用元)也省去了还要用math.round四舍五入,把double类型数据只留小数点
- 这几天恰好和朋友谈起了递归,忽然发现不少朋友对于“尾递归”的概念比较模糊,网上搜索一番也没有发现讲解地完整详细的资料,于是写了这么一篇文章,
- 平时开发,基本不改变的常量我们都放在了配置项里,如properties或yml文件里,这个时候为了只在启动时候进行加载。如何做呢?我们通过s