zoukankan      html  css  js  c++  java
  • 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).

    源代码为:

     1 public class PrintListFromTailToHead {
     2 
     3     public static void main(String[] args) {
     4         ListNode one = new ListNode(1);
     5         ListNode two = new ListNode(2);
     6         ListNode three = new ListNode(3);
     7         one.next = two;
     8         two.next = three;
     9         
    10         ArrayList<Integer> result = printListFromTailToHead(one);
    11 
    12         System.out.println("结果是:" + result);
    13     }
    14     
    15      class ListNode {
    16 
    17         public int val;
    18         public ListNode next;
    19 
    20         public ListNode() {
    21 
    22         }
    23 
    24         public ListNode(int val) {
    25             this.val = val;
    26         }
    27     }
    28 
    29     public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
    30 
    31         Stack<Integer> stack = new Stack<Integer>();
    32         while (listNode != null) {
    33             stack.push(listNode.val);
    34             listNode = listNode.next;
    35         }
    36 
    37         ArrayList<Integer> arrayList = new ArrayList<Integer>();
    38         while (!stack.isEmpty()) {
    39             arrayList.add(stack.pop());
    40         }
    41         return arrayList;
    42     }   
    45 }

    问题解释:

    代码中,我的ListNode类是定义在PrintListFromTailToHead类中的内部类。ListNode内部类是动态的内部类,而我的main方法是static静态的。

    就好比静态的方法不能调用动态的方法一样。

    有两种解决办法:

    第一种:

    将内部类ListNode定义成静态static的类。

    第二种:

    将内部类ListNode在PrintListFromTailToHead类外边定义。

    两种解决方法:

    第一种:

     1 public class PrintListFromTailToHead {
     2 
     3     public static void main(String[] args) {
     4         ListNode one = new ListNode(1);
     5         ListNode two = new ListNode(2);
     6         ListNode three = new ListNode(3);
     7         one.next = two;
     8         two.next = three;
     9         
    10         ArrayList<Integer> result = printListFromTailToHead(one);
    11 
    12         System.out.println("结果是:" + result);
    13     }
    14     
    15     static class ListNode {
    16 
    17         public int val;
    18         public ListNode next;
    19 
    20         public ListNode() {
    21 
    22         }
    23 
    24         public ListNode(int val) {
    25             this.val = val;
    26         }
    27     }

    第二种:

     1 public class PrintListFromTailToHead {
     2 
     3     public static void main(String[] args) {
     4         ListNode one = new ListNode(1);
     5         ListNode two = new ListNode(2);
     6         ListNode three = new ListNode(3);
     7         one.next = two;
     8         two.next = three;
     9     }
    10 
    11         public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
    12 
    13         Stack<Integer> stack = new Stack<Integer>();
    14         while (listNode != null) {
    15             stack.push(listNode.val);
    16             listNode = listNode.next;
    17         }
    18 
    19         ArrayList<Integer> arrayList = new ArrayList<Integer>();
    20         while (!stack.isEmpty()) {
    21             arrayList.add(stack.pop());
    22         }
    23         return arrayList;
    24     }
    25 }
    26 
    27 class ListNode {
    28 
    29     public int val;
    30     public ListNode next;
    31 
    32     public ListNode() {
    33 
    34     }
    35 
    36     public ListNode(int val) {
    37         this.val = val;
    38     }
    39 }
  • 相关阅读:
    linux配置ssh互信
    查看LINUX进程内存占用情况
    RSync实现文件备份同步详解
    rsync同步完整配置
    Linux下利用rsync实现多服务器文件同步
    Linux下的split 命令(将一个大文件根据行数平均分成若干个小文件)
    Linux大文件分割split和合并cat使用方法
    Linux计划任务入门详解
    一步一步理解最大熵模型
    一步一步理解word2Vec
  • 原文地址:https://www.cnblogs.com/lfeng1205/p/5700735.html
Copyright © 2011-2022 走看看