Java解决No enclosing instance of type PrintListFromTailToHead is accessible问题的两种方案

 更新时间:2016年07月24日 15:35:56   作者:凌风1205  
这篇文章主要介绍了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问题的两种方案,希望对大家有所帮助。

相关文章

  • Springboot如何设置过滤器及重复读取request里的body

    Springboot如何设置过滤器及重复读取request里的body

    这篇文章主要介绍了Springboot如何设置过滤器及重复读取request里的body,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • springboot页面国际化配置指南

    springboot页面国际化配置指南

    听起来高大上的国际化,起始就是在利用浏览器语言,或者页面中的中英文切换,将页面的文字在其他语言和中文进行切换,这篇文章主要给大家介绍了关于springboot页面国际化配置的相关资料,需要的朋友可以参考下
    2022-03-03
  • Java中跨域问题解决的几种方式举例详解

    Java中跨域问题解决的几种方式举例详解

    这篇文章主要介绍了前后端分离项目中跨域问题的解决方法,包括设置响应头信息、使用iframe、WebSocket、HttpServletResponse添加头信息以及通过配置类等多种方式,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-12-12
  • Idea插件安装和管理方式

    Idea插件安装和管理方式

    这篇文章主要介绍了Idea插件安装和管理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • Java调用Shell命令和脚本的实现

    Java调用Shell命令和脚本的实现

    这篇文章主要介绍了Java调用Shell命令和脚本的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • 使用JMX监控Zookeeper状态Java API

    使用JMX监控Zookeeper状态Java API

    今天小编就为大家分享一篇关于使用JMX监控Zookeeper状态Java API,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • 基于ClasspathResource路径问题的解决

    基于ClasspathResource路径问题的解决

    这篇文章主要介绍了ClasspathResource路径问题的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Java源文件命名规则详解

    Java源文件命名规则详解

    这篇文章主要介绍了Java源文件命名规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • 如何通过properties文件配置web.xml中的参数

    如何通过properties文件配置web.xml中的参数

    这篇文章主要介绍了如何通过properties文件配置web.xml中的参数方法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • SpringBoot+阿里云OSS实现在线视频播放的示例

    SpringBoot+阿里云OSS实现在线视频播放的示例

    这篇文章主要介绍了SpringBoot+阿里云OSS实现在线视频播放的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11

最新评论