新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
java項目如何使用鏈表-創(chuàng)新互聯(lián)
創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務器提供商,新人活動買多久送多久,劃算不套路!
今天就跟大家聊聊有關(guān)java項目如何使用鏈表,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
java 中鏈表的定義與使用方法
Java實現(xiàn)鏈表主要依靠引用傳遞,引用可以理解為地址,鏈表的遍歷多使用遞歸,這里我存在一個疑問同一個類的不同對象的的相同方法的方法內(nèi)調(diào)用算不算遞歸.
這里我寫的是單向鏈表;
實例代碼:
package com.example.java; public class MyLink { public static void main(String [] args){ Link l=new Link(); mytype[] la; mytype dsome=new mytype("韓敏","dsome",21); mytype shao=new mytype("邵曉","john",45); mytype hua=new mytype("華曉風","jam",46); mytype duo=new mytype("余小風","duo",1000); mytype wang=new mytype("王秋","jack",21); mytype shi=new mytype("韓寒","bob",3000); mytype yu=new mytype("于冬","keven",30); l.add(dsome);//測試增加節(jié)點 l.add(shao); l.add(hua); l.add(wang); l.add(shi); l.add(duo); l.add(yu); System.out.println("鏈表長度:"+l.length());//鏈表長度 la=l.toArray(); for(int i=0;i=this.count||index<0){ System.out.print("越界錯誤");//測試用 return null; }else{ this.foot=0; return this.root.getNode(index); } } public boolean contains(mytype data){//判斷鏈表數(shù)據(jù)是否含data if(data==null) return false; return this.root.iscontain(data); } public void remove(mytype data){//刪除指定數(shù)據(jù)節(jié)點 if(this.contains(data)){ if(this.root.data.equals(data)){ this.root=this.root.next; this.count--; } else{ this.count--; this.root.next.removeNode(root,data); } }else{ System.out.print("刪除錯誤");//測試用 } } public mytype[] toArray(){//把鏈表轉(zhuǎn)化成對象數(shù)組 if(this.count==0){ return null; } this.foot=0; this.Larray=new mytype [this.count]; this.root.toArrayNode(); return this.Larray; } } package com.example.java; public class mytype { private String name; private String people; private int age; public mytype(String name,String people,int age){//鏈表中的數(shù)據(jù)(可自定義) this.name=name; this.people=people; this.age=age; } public boolean equals(mytype data){//判斷數(shù)據(jù)是否相同 if(this==data){ return true; } if(data==null){ return false; } if(this.name.equals(data.name)&&this.people.equals(data.people)&&this.age==data.age){ return true; }else{ return false; } } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPeople() { return people; } public void setPeople(String people) { this.people = people; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getInfo(){ return "名字 :"+this.name+"\n"+ "人物 :"+this.people+"\n"+ "年齡 :"+this.age; } }
網(wǎng)站題目:java項目如何使用鏈表-創(chuàng)新互聯(lián)
URL鏈接:http://www.ef60e0e.cn/article/cdsepj.html