激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

香港云服务器
服務(wù)器之家 - 編程語言 - JAVA教程 - Java解決No enclosing instance of type PrintListFromTailToHead is accessible問題的兩種方案

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

2020-05-31 13:26凌風(fēng)1205 JAVA教程

這篇文章主要介紹了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).

源代碼為:

?
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("結(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類外邊定義。

兩種解決方法:

第一種:

?
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("結(jié)果是:" + 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

延伸 · 閱讀

精彩推薦
552
主站蜘蛛池模板: 久久人人爽人人爽人人片av免费 | 青青国产在线视频 | 国产亚洲精品久久久久久久 | 毛片免费视频播放 | 91av在线免费播放 | 黄色片免费看网站 | 极品美女一级毛片 | 久夜tv | 免费看污视频在线观看 | 高清av免费 | 国产一级大片 | 久久羞羞 | 精品久久久久久国产三级 | 中文字幕在线亚洲精品 | 精品亚洲综合 | 免费高清一级欧美片在线观看 | 欧美综合在线观看视频 | 国产成人在线观看免费网站 | 福利在线小视频 | h色视频在线观看 | 国产激情精品一区二区三区 | 亚洲热线99精品视频 | 在线观看中文字幕av | 一级视频片 | 91久久91久久精品免观看 | 日本在线高清 | 视频一区 中文字幕 | 黄色电影免费网址 | 国产欧美精品综合一区 | 精品一区二区三区在线观看国产 | 亚洲va在线 | 午夜爽爽爽男女免费观看hd | 在线中文字幕观看 | 天天骑夜夜操 | 欧美 日本 在线 | 日韩av影片在线观看 | 蜜桃91麻豆 | 九一成人 | 一级一片免费看 | 精品无码久久久久久国产 | 成人精品aaaa网站 |