如何在程序外部使用内部类
时间:2011-12-23
来源:互联网
我写了这样一个程序
涂红部位是一个内部类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 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Chain chain1=new Chain();
chain1.add(new Node("student"));
chain1.add(new Node("teacher"));
}
}
我在用new Node时报错,我应该怎么办呢????
涂红部位是一个内部类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 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Chain chain1=new Chain();
chain1.add(new Node("student"));
chain1.add(new Node("teacher"));
}
}
我在用new Node时报错,我应该怎么办呢????
作者: sdujava2011 发布时间: 2011-12-23
Node node = new Chain().new Node("");
作者: Chenhaijing 发布时间: 2011-12-23
chain1.add(chain1.new Node("student"));
chain1.add(chain1.new Node("teacher"));
chain1.add(chain1.new Node("teacher"));
作者: liu__shu 发布时间: 2011-12-23
引用 1 楼 chenhaijing 的回复:
Node node = new Chain().new Node("");
Node node = new Chain().new Node("");
就是这样的
ps:贴代码的时候注意格式
作者: Chianfirstli 发布时间: 2011-12-23
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28