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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国语自产拍在线播放不卡 | 顶级尤物极品女神福利视频 | 欧乱色国产精品兔费视频 | 亚欧毛片基地国产毛片基地 | 91亚色视频在线观看 | 久久黄色精品视频 | 俄罗斯女人与公拘i交酡 | 亚洲男人天堂网站 | 99视频精品免费99在线 | 日产乱码卡1卡2卡三卡四在线 | 午夜爽喷水无码成人18禁三级 | 俺去俺来也在线www色官网 | 日韩天堂视频 | 99视频都是精品热在线播放 | 奇米777四色精品综合影院 | 狠狠色综合久久婷婷 | 香蕉视频在线观看网址 | 日本高清免费观看 | 国产精品林美惠子在线观看 | 亚洲性久久久影院 | 精品久久久久免费极品大片 | 欧美人妖另类性hd | 国产a片毛片 | 男人好大好硬好爽免费视频 | 91aaa在线观看 | 69热精品视频在线看影院 | 欧美性xxx狂流白浆 欧美性f | 国产在线精品成人一区二区三区 | 脱了白丝校花的内裤猛烈进入 | 精品区卡一卡2卡三免费 | 日韩在线免费看 | 欧美a级v片不卡在线观看 | 国产日韩欧美 | 成人18视频在线观看 | 粉嫩高中生第一次不戴套 | 91传媒制片厂果冻有限公司 | 亚洲精品一区二区三区中文字幕 | 国产精品66福利在线观看 | 亚洲国产精品婷婷久久久久 | 动漫人物差差插曲漫画 | 好大好猛好爽好深视频免费 |