求助~(java链表关于对象的引用,与传递和修改。)硬是没看懂

tifacxbc 2016-08-22 04:13:34
class Link{
public int iDate;
public double dDate;
public Link next;
public Link(int i,double d){
iDate=i;
dDate=d;
}
public void displayLink(){
System.out.println("iDate=="+iDate+", dDate=="+dDate);
}
}
public class LinkList {

/**
* @param args
*/
public Link first;
public LinkList(){
first=null;
}
public void insertFirst(int i,double d){
Link newLink=new Link(i,d);
newLink.next=first;
first=newLink;
}
public void displayLink(){
System.out.println("链表显示");
Link curren=first;
while(curren!=null){
curren.displayLink();
curren=curren.next;

}
}
public void test(){
Link curren=first;
System.out.println(curren.hashCode()+"~~~~~~~"+first.hashCode());//测试结果 7051261~~~~~~~7051261

/*first=first.next;
System.out.println(curren.hashCode()+"~~~~~~~"+first.hashCode());//测试结果 7051261~~~~~~~29855319
*/


/* Link pre=first.next;
pre=first.next.next;

first.next=first.next.next;*/ // 这两个代码的执行结果有撒区别

first.next=first.next.next;
System.out.println(curren.hashCode()+"~~~~~~~"+first.hashCode());//测试结果 7051261~~~~~~~7051261


}
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkList ll=new LinkList();
ll.insertFirst(1, 88);
ll.insertFirst(2, 99);
ll.insertFirst(3, 100);
ll.insertFirst(4, 105);
ll.insertFirst(5, 323);
ll.test();
}
}

重点test()方法为什么两次的测试结果不一样,
/*first=first.next;
System.out.println(curren.hashCode()+"~~~~~~~"+first.hashCode());//测试结果 7051261~~~~~~~29855319
*/


first.next=first.next.next;
System.out.println(curren.hashCode()+"~~~~~~~"+first.hashCode());//测试结果 7051261~~~~~~~7051261
...全文
307 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tifacxbc 2016-08-22
  • 打赏
  • 举报
回复
顶下,代码有段乱,其实很简单,全部复制就能直接运行。 Link curren=first; Link t; curren.next=curren.next.next;能起到修改first链表结构的作用 而curren=curren.next不能起到修改链表的值的作用; 再或者 t=curren.next; t=t.next; 不能修改first链表结构 和 curren.next=curren.next.next;有撒区别。。能起到修改first链表结构的作用 但 t=curren.next; t.next=t.next.next;能起到修改first链表结构值的作用; 通过以上这些测试得到的结论,但是对对象的引用这方面知识不是很透彻;有没有大神知道,为什么会有上面的测试结果
  • 打赏
  • 举报
回复
引用 1 楼 tifacxbc 的回复:
原来简单的链表结构,扫了一眼书上的代码。感觉没有撒难度。 就是不太清楚 first.next=first.next.next;//curren.next=curren.next.next; first=first.next;//curren=curren.next; 这两个有撒区别,为什么结果不同
curren = first 这个时候指定了curren 的值,所以 不管你下面怎么用 curren的hashcord是一样的。 next一次 就相当于指针后移一次, 所以你next 和 next.next 的值肯定不一样
tifacxbc 2016-08-22
  • 打赏
  • 举报
回复
原来简单的链表结构,扫了一眼书上的代码。感觉没有撒难度。 就是不太清楚 first.next=first.next.next;//curren.next=curren.next.next; first=first.next;//curren=curren.next; 这两个有撒区别,为什么结果不同

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧