Java solves the problem of No enclosing instance of type PrintListFromTailToHead is accessible, enclosinginstance

Source: Internet
Author: User

Java solves the problem of No enclosing instance of type PrintListFromTailToHead is accessible, enclosinginstance

The following problems are encountered during Java program compilation today:

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 ).

Source code:

 

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:" + result); 13} 14 15 class ListNode {16 17 public int val; 18 public ListNode nex T; 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}

 

Explanation:

In the code, my ListNode class is an internal class defined in the PrintListFromTailToHead class. The internal class of ListNode is a dynamic internal class, and my main method is static.

Just like static methods cannot call dynamic methods.

There are two solutions:

First:

Define the internal class ListNode as a static class.

Second:

Define the internal class ListNode outside the PrintListFromTailToHead class.

 

Two solutions:

First:

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:" + 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}

Second:

 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 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.