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內部類是動態(tài)的內部類,而我的main方法是static靜態(tài)的。
就好比靜態(tài)的方法不能調用動態(tài)的方法一樣。
有兩種解決辦法:
第一種:
將內部類ListNode定義成靜態(tài)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自動掃描添加的BeanDefinition源碼實例詳解
這篇文章主要給大家介紹了關于springboot自動掃描添加的BeanDefinition的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-02-02Spring整合SpringMVC與Mybatis(SSM)實現(xiàn)完整登錄功能流程詳解
開學學校開始講servlet 后期要求做一個登錄功能,這個使用SSM先只做個簡單的只帶登錄功能的,且項目使用了MyBatis-Plus來簡化開發(fā)流程??辞闆r決定要不要升級功能或者換個寫法2022-09-09詳解mybatis 批量更新數(shù)據(jù)兩種方法效率對比
這篇文章主要介紹了詳解mybatis 批量更新數(shù)據(jù)兩種方法效率對比,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-02-02配置hadoop環(huán)境mapreduce連接不上hdfs解決
這篇文章主要為大家介紹了配置hadoop環(huán)境mapreduce連接不上hdfs解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10SpringBoot整合Redis使用RedisTemplate和StringRedisTemplate
Spring?Boot?Data(數(shù)據(jù))?Redis?中提供了RedisTemplate和StringRedisTemplate,其中StringRedisTemplate是RedisTemplate的子類,兩個方法基本一致。本文介紹了SpringBoot整合Redis使用RedisTemplate和StringRedisTemplate的方法,需要的可以參考一下2022-12-12Java面試題沖刺第十三天--數(shù)據(jù)庫(3)
這篇文章主要為大家分享了最有價值的三道數(shù)據(jù)庫面試題,涵蓋內容全面,包括數(shù)據(jù)結構和算法相關的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下2021-07-07