一个数据结构的小问题! 汗,,,,a.next=b;b.next=c;...另:String n=args[0]; String m=args[1];有问题,用for循环取args的元素 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 开始时你的a.next为null, b=a.next后,b为null,再执行c=b.next;肯定会报NullPointerException应该是a.next=b,.... 给你写了吧class Lianbiao1 //计算任两节点间的距离 { static class Node {String val;Node next; Node(String v) {val=v;} } public static void main(String[] args) { char n=args[0]; char m=args[1]; if(n>m)//swap n, m { char temp=n; n=m; m=temp; } Node a=new Node("a"); Node b=new Node("b"); Node c=new Node("c"); Node d=new Node("d"); Node e=new Node("e"); Node f=new Node("f"); Node g=new Node("g"); Node h=new Node("h"); a.next=b; b.next=c; c.next=d; d.next=e; e.next=f; f.next=g; g.next=h; Node x=a; int i=0; while(x!=null&&x.val!=n) {x=x.next;} if(x==null) { System.out.println("the first node does not exist!"); System.exit(0); } while(x.val!=m) {x=x.next; i++;} if(x==null) { System.out.println("the second node does not exist!"); System.exit(0); } System.out.println("the numbers between the nodes is:"+i); }}如果需要实现可连续接受值,就在开始处用While循环控制,退出程序只要输入不存在的节点就可以了 Java程序如何操作Indesign排版软件 学struts 时,那些是重要的? 【项目求助】ireport0.5.1,能做到不用附加的Scriptlet类,完全实现它内置的那些chart所有的功能吗?急切…… 、绝对优质JAVA版本OA系统技术转让!!! 字符的判断问题 请问做JAVA一般都用什么数据库?全是ORACLE吗? 问一个mysql事务并发的问题 jcreator问题求救 有谁有Anfy的注册号 java问题???怎么解决 Jar包又一问?!! Java集合类问题?
肯定会报NullPointerException
应该是a.next=b,....
class Lianbiao1 //计算任两节点间的距离
{ static class Node
{String val;Node next;
Node(String v) {val=v;}
} public static void main(String[] args)
{
char n=args[0];
char m=args[1];
if(n>m)//swap n, m
{ char temp=n;
n=m;
m=temp;
} Node a=new Node("a");
Node b=new Node("b");
Node c=new Node("c");
Node d=new Node("d");
Node e=new Node("e");
Node f=new Node("f");
Node g=new Node("g");
Node h=new Node("h");
a.next=b;
b.next=c;
c.next=d;
d.next=e;
e.next=f;
f.next=g;
g.next=h;
Node x=a;
int i=0;
while(x!=null&&x.val!=n)
{x=x.next;}
if(x==null)
{
System.out.println("the first node does not exist!");
System.exit(0);
}
while(x.val!=m)
{x=x.next;
i++;}
if(x==null)
{
System.out.println("the second node does not exist!");
System.exit(0);
} System.out.println("the numbers between the nodes is:"+i);
}
}
如果需要实现可连续接受值,就在开始处用While循环控制,退出程序只要输入不存在的节点就可以了