如何在程序外部使用内部类 我写了这样一个程序 涂红部位是一个内部类Node public class Chain { public class Node { private String data; private Node next; public Node(String str,Node n) { data=str; next=n; }
public Node (String str) { data=str; } } private Node first=null; public void addFirst(Node n){ n.next=first; first=n; } public int length(){ int count =0; Node temp=first; if(temp==null) return 0; else{ while(temp.next!=null) count++; return count; } } public void add(Node n){ Node temp=first; if(first==null) first=n; else{ String a=temp.data; String b=n.data; while(a.compareTo(b)<0) temp=temp.next; temp.next=n; } } public void addIndex(int a,Node n){ Node temp=first; if(first==null){ first=n; } else{ int count=0; while(count<a){ temp=temp.next; } temp.next=n; } } public void deleteIndex(int a){ if(first==null) System.out.println("链表内内容为空!"); else{ Node temp=first; Node temp2=first.next.next; int count=0; while(count<a-1) temp=temp.next; temp.next=temp2; } } public Chain combine(Chain c){ Node temp1=this.first; Node temp2=c.first; Chain chain=new Chain(); if(temp1==null) return c; if(temp1!=null&&temp2!=null){ int i; int l1=this.length(); int l2=c.length(); if(l1<=l2) i=l1; else i=l2; for(int count=1;count<=i;count++){ if(temp1.data.compareTo(temp2.data)<0){ chain.add(temp1); temp1=temp1.next; } else{ chain.add(temp2); temp2=temp2.next; } } if(l1<l2){ int diff=l2-l1; for(int n=1;n<=diff;n++){ chain.add(temp2); temp2=temp2.next; } } else { int diff=l1-l2; for(int n=1;n<=diff;n++){ chain.add(temp1); temp1=temp1.next; } } return chain; } else return this; } } 下面是一个驱动类 public class Test {