首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 笔记本电脑 | 北大青鸟 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Linux频道
中国IT教育
Google
首页 资讯动态 认证考试 新手入门 核心技术 高级技术 J2EE J2ME Java&XML 开源技术 其他技术 RSS订阅 论坛 专题
您现在的位置: 中国IT实验室 >> Java >> 新手入门 >> 基础入门 >> 正文

Java数据结构---基于数组的表


   
  return (str.compareTo(newe.str));
 }

 /* (non-Javadoc)
  * @see DataElement#makeCopy(DataElement)
  */
 public void makeCopy(DataElement otherElement) {
  // TODO Auto-generated method stub
  StringElement newe=(StringElement)otherElement;
  str=newe.str;
 }

 /* (non-Javadoc)
  * @see DataElement#getCopy()
  */
 public DataElement getCopy() {
  // TODO Auto-generated method stub
  
  StringElement othere=new StringElement();
  othere.str=str;
  return othere;
  
 }

 public String toString(){
  return str;
 }
}

已经定义好了数据类型,所以list的数据类型我们就可以定义为DateElement[]了,这样就可以包括所以你想要的了,只要你在用的时候定义一个DataElement的子类就行了,这正是java继承的精髓所在.我们接着定义ArrayListClass类:

 protected int length;
 protected int maxSize;
 protected DataElement[] list;这就是它的所有域了.

接下来就是它的方法了,我们对表的操作应该有很多种,比如插入、查询、删减等等,我们要逐个的实现,具体方法不再赘述,且看最后完成代码

public abstract class  ArrayListClass {
 //fields
 protected int length;
 protected int maxSize;
 protected DataElement[] list;
 
 //defalt constructors
 public ArrayListClass(){
  length=0;
  maxSize=100;
  list=new DataElement[maxSize];
 }
 //constructors
 public ArrayListClass(int size){
  if(size<=0){
   System.err.println("The arry size must be positive.Creating an array of size 100.");
   maxSize=100;
  }
  else 
   maxSize=size;
  length=0;
  list=new DataElement[maxSize];
 }
 public ArrayListClass(ArrayListClass otherList){
  maxSize=otherList.maxSize;
  length=otherList.length;
  list=new DataElement[maxSize];
  for(int i=0;i<length;i++){
   list[i]=otherList.list[i].getCopy();
  }
 }
 
 //methods
 public boolean isEmpty(){
  return (length==0);
 }
 public boolean isFull(){
  return (length==maxSize);
 }
 public int listSize(){
  return length;
 }
 public int maxListSize(){
  return maxSize;
 }
 public void print(){
  for(int i=0;i<length;i++){
   System.out.print(list[i]+" ");
  }
  System.out.println();
 }
 public boolean isItemAtEqual(int location,DataElement item){
  return(list[location].equals(item));
 }
 public void insrtAt(int location,DataElement insertItem){
  if(location<0||location>+maxSize){
   System.out.println("The position of the item to be inserted is out of range!!");
  }
  else 
   if(length>=maxSize)
    System.err.println("Can’t insert in a full list!!");
   else{
    for(int i=length;i>location;i--){
     list[i]=list[i-1];
    }
     list[location]=insertItem.getCopy();
     length++;
   }
 }
 public void insertEnd(DataElement insertItem){
  if(length>=maxSize){
   System.err.println("Can’t insert in a full list!!");
  }
  
  else{
   list[length]=insertItem.getCopy();
  length++;
  }
 }
 public void removeAt(int location){
  if(location<0||location>=length){
   System.err.println("The location you want to remove is out of range!!");
  }
  else{
   for(int i=location;i<length-1;i++){
    list[i]=list[i+1];
   }
   list[length]=null;
   length--;
  }
 }
    public DataElement retrieveAt(int location){
     if(location<0||location>=length){
      System.err.println("The location of item to be retrieved is out of range!!");
         return null;
     }
     else{
      return list[location].getCopy();
     }
    }
    public void replacAt(int location,DataElement repItem){
     if(location<0||location>=length)
      System.out.println("The position of item to be replaced is out of range!!");
     else
      list[location]=repItem.getCopy();
    }
    public void clearList(){
     for(int i=0;i<length;i++){
      list[i]=null;
     }
     length=0;
     System.gc();
    }
 

上一页  [1] [2] [3] 下一页

【责编:Peng】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·C++箴言大合集
 ·开源框架 Structs 2.0入门教程
 ·诱人的奶酪 J2ME手机开发技术
 ·Oracle外键及外键约束修改行为
 ·开源软件测试工具学习专题
 ·JSP Web开发 入门基础到高手进阶教程
 ·JavaFX—是Java桌面的新希望么?
 ·安全至上 .NET开发安全策略
 ·测试用例设计之道-测试用例学习专题
 ·面向Java开发人员的Scala指南
 今日更新
 社区讨论
 博客论点
 频道精选
 Java 频道导航