Java 程序时遇到如下问题 程序时遇到如下问题:
Nenhuma instância de gabinete do tipo printlistFromtailToHead é acessível. Deve qualificar a alocação com uma instância de anexo
do tipo printlist de BothToTohead (EgxNew A () em que X é uma instância de PrintListFromtailToHead).
源代码为
public class PrintListFromtailToHead {public static void main (string [] args) {listNode One = new ListNode (1); listNode dois = new ListNode (2); listNode três = new ListNode (3); PrintFrent = dois; dois.next = três; + resultado);} classe listNode {public int val; public listNode Next; public listNode () {} public listNode (int val) {this.val = val;}} public static ArrayList <TelingNeLin) (PrintlistFromtailhead (listNode) {Stack <Integer> Stack = newstack <Tack = new) {Stack.push (listnode.val); listnode = listnode.next;} ArrayList <Teger> ArrayList = new ArrayList <Teger> (); while (! Stack.isEmpty ()) {Arraylist.add (stak.pop ());} retorno;问题解释
代码中 , 我的 listNode 类是定义在 printlistfromtailToHead 类中的内部类。Listnode 内部类是动态的内部类 , 而我的 Principal 方法是 estático 静态的。
就好比静态的方法不能调用动态的方法一样。
有两种解决办法
第一种
将内部类 ListNode 定义成静态 estático 的类。
第二种
将内部类 ListNode 在 printlistFromtailToHead 类外边定义。
两种解决方法
第一种
public class PrintListFromtailToHead {public static void main (string [] args) {listNode One = new ListNode (1); listNode dois = new ListNode (2); listNode três = new ListNode (3); PrintFrent = dois; dois.next = três; + resultado);} classe estática listNode {public int val; public listNode Next; public listNode () {} public listNode (int val) {this.val = val;}}第二种
classe public PrintListFromtailToHead {public static void main (string [] args) {listNode One = new ListNode (1); listNode dois = new ListNode (2); listNode Three = new ListNode (3); One.Next = dois; dois.next = três;} Public Static ArrayList <intereger> {Stack <Integer> Stack = new Stack <Teger> (); while (listnode! = Null) {staack.push (listnode.val); listnode = listnode.next;} ArrayList <Tackisemm> ArrayList = new Arraylist <Tegerger> (); {ArrayList.add (Stack.pop ());} retornar ArrayList;}} classe listNode {public int val; public listNode Next; public listNode () {} public listNode (int val) {this.val = val;}}以上所述是小编给大家介绍的 Java 解决 Nenhuma instância de anexo do tipo printlistfromtailtohead é acessível 问题的两种方案 , 希望对大家有所帮助。