java调用系统包的链表_Java 单向链表学习

class Province { //每一个类就相当于数据库中的一个表;

private intpid ;privateString name ;private Link cities = new Link() ; //一对多//setter getter 无参构造 略~

public Province(intpid , String name) {this.pid =pid ;this.name =name ;

}publicLink getCities() {return this.cities ;

}publicString getInfo() {return "省份编号:" + this.pid + ", 名称:" + this.name ;

}

}classCity {private intcid ;privateString name ;private Province province ; //省份对象元素

public City(intcid , String name) {this.cid =cid ;this.name =name ;

}public booleancompare(City data) {if ( this ==data ) {return true;

}if ( data == null) {return false;

}if ( this.cid == data.cid && this.name.equals(data.name)) {return true;

}return false;

}public voidsetProvince(Province province) {this.province =province ;

}publicProvince getProvince() {return this.province;

}publicString getInfo() {return "城市编号:" + this.cid + ", 名称:" + this.name ;

}

}/*每一个实例化的对象都是单独的个体的存在,占用的是独立的内存空间

所以每一个实例对象的操作不影响其它实例对象或者类的数据*/

classLink {classNode {privateCity data ;privateNode next ;publicNode(City data) {this.data =data;

}public voidaddNode(Node newNode) {if ( this.next == null) {this.next =newNode ;

}else{this.next.addNode(newNode) ;

}

}public booleancontainsNode(City data) {if ( data.compare(this.data)) {return true;

}else{if ( this.next != null){return this.next.containsNode(data) ;

}else{return false;

}

}

}public City getNode(intindex) {if ( Link.this.foot ++ ==index ) {return this.data ;

}else{return this.next.getNode(index) ;

}

}public void setNode(intindex , City data) {if ( Link.this.foot ++ ==index ) {this.data =data ;

}else{this.next.setNode(index , data) ;

}

}public voidremoveNode(Node previous , City data) {if ( data.compare(this.data)){

previous.next= this.next ; //空掉当前(this)的节点

}else{if ( this.next != null) {this.next.removeNode(this,data) ;

}

}

}public voidtoArrayNode() {

Link.this.cityArray[Link.this.foot ++ ] = this.data ;if ( this.next != null) {this.next.toArrayNode() ;

}

}

}privateNode root ;private int count = 0;private int foot = 0;privateCity [] cityArray ;public voidadd(City data) {

Node newNode= newNode(data) ;if ( this.root == null) {this.root =newNode ;

}else{this.root.addNode(newNode) ;

}this.count ++;

}public intsize() {return this.count ;

}public booleanisEmpty() {return count == 0;

}public booleancontains(City data) {if ( data.compare(this.root.data) ) { //**--

return true;

}else{return this.root.containsNode(data) ;

}

}public City get(intindex) {if ( index >count ){return null;

}this.foot = 0;return this.root.getNode(index) ;

}public void set(intindex , City data) {if ( index >count ){return;

}this.foot = 0;this.root.setNode(index , data) ;

}public voidremove(City data) {if ( data.compare(this.root.data) ) {this.root = this.root.next ;

}this.root.removeNode(this.root , data) ;this.count --;

}publicCity [] toArray() {if ( this.root == null) {return null;

}this.foot = 0;this.cityArray= new City[this.count] ;this.root.toArrayNode() ;returncityArray ;

}

}public classLinkPC {public static voidmain(String args[]) {//设置关系数据

Province pro= new Province(1,"江苏省") ; //声明Province类对象

City c1= new City(1001,"南京市") ;

City c2= new City(1002,"苏州市") ;

City c3= new City(1003,"宿迁市") ; //什么多个City类对象//设置关系

c1.setProvince(pro) ;//利用City实例对象c1调用setProvince()方法并将pro对象传递

c2.setProvince(pro) ;//这就是所谓的 "引用传递"

c3.setProvince(pro) ;

pro.getCities().add(c1) ;

pro.getCities().add(c2) ;

pro.getCities().add(c3) ;

System.out.println(pro.getInfo());

System.out.println(pro.getCities().size()) ;

City [] ci=pro.getCities().toArray() ;for ( int x = 0 ; x < ci.length ; x ++) {

System.out.println(ci[x].getInfo()) ;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值