最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java解決No enclosing instance of type PrintListFromTailToHead is accessible問題的兩種方案

 更新時(shí)間:2016年07月24日 15:35:56   作者:凌風(fēng)1205  
這篇文章主要介紹了Java解決No enclosing instance of type PrintListFromTailToHead is accessible問題的兩種方案的相關(guān)資料,需要的朋友可以參考下

今天在編譯Java程序時(shí)遇到如下問題:

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("結(jié)果是:" + 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類中的內(nèi)部類。ListNode內(nèi)部類是動(dòng)態(tài)的內(nèi)部類,而我的main方法是static靜態(tài)的。

就好比靜態(tài)的方法不能調(diào)用動(dòng)態(tài)的方法一樣。

有兩種解決辦法:

第一種:

將內(nèi)部類ListNode定義成靜態(tài)static的類。

第二種:

將內(nèi)部類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("結(jié)果是:" + 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問題的兩種方案,希望對(duì)大家有所幫助。

相關(guān)文章

  • Springboot如何設(shè)置過濾器及重復(fù)讀取request里的body

    Springboot如何設(shè)置過濾器及重復(fù)讀取request里的body

    這篇文章主要介紹了Springboot如何設(shè)置過濾器及重復(fù)讀取request里的body,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • springboot頁面國際化配置指南

    springboot頁面國際化配置指南

    聽起來高大上的國際化,起始就是在利用瀏覽器語言,或者頁面中的中英文切換,將頁面的文字在其他語言和中文進(jìn)行切換,這篇文章主要給大家介紹了關(guān)于springboot頁面國際化配置的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • Java中跨域問題解決的幾種方式舉例詳解

    Java中跨域問題解決的幾種方式舉例詳解

    這篇文章主要介紹了前后端分離項(xiàng)目中跨域問題的解決方法,包括設(shè)置響應(yīng)頭信息、使用iframe、WebSocket、HttpServletResponse添加頭信息以及通過配置類等多種方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-12-12
  • Idea插件安裝和管理方式

    Idea插件安裝和管理方式

    這篇文章主要介紹了Idea插件安裝和管理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java調(diào)用Shell命令和腳本的實(shí)現(xiàn)

    Java調(diào)用Shell命令和腳本的實(shí)現(xiàn)

    這篇文章主要介紹了Java調(diào)用Shell命令和腳本的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • 使用JMX監(jiān)控Zookeeper狀態(tài)Java API

    使用JMX監(jiān)控Zookeeper狀態(tài)Java API

    今天小編就為大家分享一篇關(guān)于使用JMX監(jiān)控Zookeeper狀態(tài)Java API,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • 基于ClasspathResource路徑問題的解決

    基于ClasspathResource路徑問題的解決

    這篇文章主要介紹了ClasspathResource路徑問題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java源文件命名規(guī)則詳解

    Java源文件命名規(guī)則詳解

    這篇文章主要介紹了Java源文件命名規(guī)則,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 如何通過properties文件配置web.xml中的參數(shù)

    如何通過properties文件配置web.xml中的參數(shù)

    這篇文章主要介紹了如何通過properties文件配置web.xml中的參數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringBoot+阿里云OSS實(shí)現(xiàn)在線視頻播放的示例

    SpringBoot+阿里云OSS實(shí)現(xiàn)在線視頻播放的示例

    這篇文章主要介紹了SpringBoot+阿里云OSS實(shí)現(xiàn)在線視頻播放的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11

最新評(píng)論

炎陵县| 皮山县| 常宁市| 邓州市| 甘洛县| 娱乐| 登封市| 永德县| 宜君县| 成都市| 吐鲁番市| 阿拉善左旗| 乌审旗| 奉新县| 永新县| 独山县| 内黄县| 巴里| 乡城县| 峨边| 敖汉旗| 连城县| 手游| 榆中县| 西峡县| 桐城市| 凌海市| 遂昌县| 宜丰县| 孙吴县| 庄浪县| 新建县| 鄯善县| 平阴县| 会同县| 三穗县| 横山县| 古田县| 五大连池市| 渝中区| 铜山县|