关闭

用java数组实现基本链表和可自扩充的链表

发表于:2009-10-12 10:44

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:网络转载

#
java

  package com.tongji.szx.base;

  import com.tongji.szx.baseDao.ListInterface;

  public class ExtendList<T> implements ListInterface<T>{

  private T[] entry;

  private int length;

  private int capacity;

  private static final int BASE_LENGTH=10;

  /**

  * 默认的构造函数

  */

  public ExtendList(){

  this(BASE_LENGTH);

  }

  @SuppressWarnings("unchecked")

  /**

  * 提供一个初始链表长度的构造函数

  */

  public ExtendList(int intLength){

  if(intLength>0){

  this.length=intLength;

  this.capacity=intLength;

  entry=(T[])new Object[intLength];

  }else{

  this.length=BASE_LENGTH;

  this.capacity=BASE_LENGTH;

  entry=(T[])new Object[BASE_LENGTH];

  }

  }

  @Override

  /**

  * 在链表的末尾插入元素

  */

  public boolean add(T anEntry) {

  // TODO Auto-generated method stub

  try{

  if(length==capacity){

  copyEntry();

  }

  entry[length]=anEntry;

  length++;

  return true;

  }catch(Exception e){

  return false;

  }

  }

  @SuppressWarnings("unchecked")

  private void copyEntry(){

  T[] newEntry=entry;

  this.capacity*=2;

  entry=(T[])new Object[this.capacity];

  for(int index=0;index<newEntry.length;++index){

  entry[index]=newEntry[index];

  }

  }

31/3123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号