今天在编译 Java 程序时遇到如下问题 :
Es ist keine eingeschlossene Instanz von Typ -PrintlistFromTailTohead zugänglich. Muss die Zuordnung mit einer umschließenden Instanz qualifizieren
vom Typ printlistfromTailTohead (EGXNew a () wobei x eine Instanz von PrintlistFromTailTohead ist).
源代码为 :
public class printlistfromTailtohead {public static void main (string [] args) {listNode ein = neuer listNode (1); listNode zwei = neuer listNode (2); listNode drei = neuer listNode (3); ein.Next = zwei; zwei.Next = drei; ArrayList <Integer 结果是 结果是. Ergebnis);} Klasse ListNode {public int val; public ListNode Weiter; public listNode () {} public ListNode (int val) {this.val = val;}} public static ArrayList <Ganzegrontile -printlistfromtailtoheads (listnode listnode) {stack <Integer> nul = nul = nun = nun nun) neuere Stack = Neue Stack) (NEW NEW); {stack.push (listNode.val); listNode = listNode.next;} ArrayList <Geingreger> ArrayList = new ArrayList <GanzEger> (); while (! stack.isempty ()) {ArrayList.Add (stack.pop ();问题解释 :
代码中 , 我的 LISTNODE 类是定义在 printlistfromTailTohead 类中的内部类。Listnode 内部类是动态的内部类 , 而我的 Main 方法是 static 静态的。
就好比静态的方法不能调用动态的方法一样。
有两种解决办法 :
第一种 :
将内部类 LISTNODE 定义成静态 STATIC 的类。
第二种 :
将内部类 LISTNODE 在 printlistfromTailTohead 类外边定义。
两种解决方法 :
第一种 :
public class printlistfromTailtohead {public static void main (string [] args) {listNode ein = neuer listNode (1); listNode zwei = neuer listNode (2); listNode drei = neuer listNode (3); ein.Next = zwei; zwei.Next = drei; ArrayList <Integer 结果是 结果是. Ergebnis);} statische Klasse ListNode {public int val; public ListNode Weiter; 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 <GeNeger> (); while (listNode! ArrayList;}} Klasse ListNode {public int val; public ListNode Weiter; public listNode () {} public ListNode (int val) {this.val = val;}}以上所述是小编给大家介绍的 Java 解决 Keine eingeschlossene Instanz der Typ -Prinzipel -FROMTAILTOHEAD ist zugänglich 问题的两种方案 , 希望对大家有所帮助。