一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java解決No enclosing instance of type PrintListFromTailToHead is accessible問題的兩種方案

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

2020-05-31 13:26凌風1205 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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類外邊定義。

兩種解決方法:

第一種:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
}
}

第二種:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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問題的兩種方案,希望對大家有所幫助。

原文鏈接:http://www.cnblogs.com/lfeng1205/archive/2016/07/24/5700735.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: www.久久艹| 1769亚洲资源站365在线 | 舔大逼| 白发在线视频播放观看免费 | 亚洲国产综合自在线另类 | 疯狂激吻添下边小说 | 亚洲福利区 | 小货SAO边洗澡边CAO你动漫 | 国产精品嫩草影院一二三区 | 免费精品一区二区三区在线观看 | 日本aaa大片| 狠狠色伊人亚洲综合网站色 | 婷婷综合缴情亚洲五月伊 | 天天做天天爱天天一爽一毛片 | а天堂中文最新版在线 | 国产一区二区精品久 | 精品免费国产一区二区三区 | 天天干天天操天天爽 | 精品国产品国语在线不卡丶 | 国产精品天天看特色大片不卡 | 校服下的白嫩小乳尖h1v1 | 国产成人久视频免费 | 韩国美女被的免费视频 | 免费观看毛片视频 | 国产成人亚洲精品乱码在线观看 | voyeur 中国女厕 亚洲女厕 | 公交车强校花系列小说 | 狠狠久久久久综合网 | 爱爱小说漫画 | 国产大秀视频 | 特大黑人娇小亚洲女mp4 | 久久99热在线观看7 久久99精品涩AV毛片观看 | 国产在线视频福利 | 久久伊人精品青青草原2021 | 色婷婷在线播放 | 亚洲精品无码不卡在线观看 | a男人的天堂久久a毛片 | 国产亚洲女在线精品 | 日韩在线观看网址 | 色综久久天天综合绕视看 | 青青热久麻豆精品视频在线观看 |