No enclosing instance of type ReverseList is accessible. Must qualify the allocation with an enclosing instance of type ReverseList (e.g. x.new A() where x is an instance of ReverseList).
1 package day2; 2 //自己定义双向链表 和 单链表结构 然后进行反转操作 3 4 public class ReverseList { 5 6 7 public class Node { 8 public int data ; 9 public Node next; 10 11 public Node (int data) { 12 this.data = data; 13 //this.next = null; 14 } 15 } 16 17 18 //反转单项链表 19 public static Node reverseList(Node head ) { 20 // 三个指针的 21 Node next = null; 22 Node pre = null; 23 while (head != null) { 24 next = head.next; 25 head.next = pre; 26 pre = head; 27 head = next; 28 } 29 return pre; 30 } 31 32 33 34 35 36 public class DoubleNode{ 37 public int data ; 38 public DoubleNode next; 39 public DoubleNode pre; 40 41 public DoubleNode (int data){ 42 this.data = data; 43 this.next = null; 44 this.pre = null; 45 } 46 } 47 48 49 //反转双向链表 50 public static DoubleNode reverseDoubleList(DoubleNode head) { 51 DoubleNode pre = null; 52 DoubleNode next = null; 53 while (head != null) { 54 next = head.next; 55 head.next = pre; 56 head.pre = next; 57 pre = head ; 58 head = next; 59 } 60 return pre; 61 } 62 63 64 public static void printLinkedList(Node head) { 65 System.out.print("Linked List: "); 66 while (head != null) { 67 System.out.print(head.data+" -> "); 68 head=head.next; 69 } 70 System.out.println("null"); 71 } 72 73 public static void printDoubleLinkedList(DoubleNode head) { 74 System.out.print("Double Linked List: "); 75 DoubleNode end = null; 76 while (head != null) { 77 System.out.print(head.data+" -> "); 78 end = head; 79 head=head.next; 80 } 81 System.out.print("null | "); 82 83 84 while (end != null) { 85 System.out.print(end.data+" -> "); 86 //end = head; 87 end = end.pre; 88 } 89 System.out.println("null"); 90 } 91 92 93 public static void main(String[] args) { 94 Node head1 = new Node(1); 95 //Node head1 = new Node(1); 96 head1.next = new Node(2); 97 head1.next.next = new Node(3); 98 printLinkedList(head1); 99 head1 = reverseList(head1); 100 printLinkedList(head1); 101 102 DoubleNode head2 = new DoubleNode(1); 103 head2.next = new DoubleNode(2); 104 head2.next.pre = head2; 105 head2.next.next = new DoubleNode(3); 106 head2.next.next.pre = head2.next; 107 head2.next.next.next = new DoubleNode(4); 108 head2.next.next.next.pre = head2.next.next; 109 printDoubleLinkedList(head2); 110 111 printDoubleLinkedList(reverseDoubleList(head2)); 112 113 } 114 115 116 117 118 119 }
我写的内部类是动态的,也就是开头以public class开头。而主程序是public static class main。在Java中,类中的静态方法不能直接调用动态方法。只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。所以在不做其他变动的情况下,最简单的解决办法是将public class改为public static class.
修改:
1 package day2; 2 //自己定义双向链表 和 单链表结构 然后进行反转操作 3 4 public class ReverseList { 5 6 7 public static class Node { 8 public int data ; 9 public Node next; 10 11 public Node (int data) { 12 this.data = data; 13 //this.next = null; 14 } 15 } 16 17 18 19 20 public static class DoubleNode{ 21 public int data ; 22 public DoubleNode next; 23 public DoubleNode pre; 24 25 public DoubleNode (int data){ 26 this.data = data; 27 this.next = null; 28 this.pre = null; 29 } 30 } 31 32 33 34 35 36 37 }