(一)线性结构之ArrayList的实现

线性结构定义

如果一个数据元素序列满足:

(1)除第一个和最后一个数据元素外,每个数据元素只有一个前驱数据元素和一个后继数据元素;

(2)第一个数据元素没有前驱数据元素;

(3)最后一个数据元素没有后继数据元素。

  则称这样的数据结构为线性结构。

线性表抽象数据类型

线性表抽象数据类型主要包括两个方面:既数据集合和该数据集合上的操作集合。
数据集合可以表示为a0,a1,a2,...an-1,每个数据元素的数据类型可以是任意的类型。
操作集合包括如下:

1.求元素个数

2.插入

3.删除

4.查找

5.判断是否为空

5.判断是否为空

设计线性表抽象数据类型的Java接口

[java]  view plain copy
  1. <span style="font-size:18px">public interface List {  
  2.     // 获得线性表长度  
  3.     public int size();  
  4.     // 判断线性表是否为空  
  5.     public boolean isEmpty();  
  6.     // 插入元素  
  7.     public void add(int index, Object obj) throws Exception;  
  8.     // 删除元素  
  9.     public void delete(int index) throws Exception;  
  10.     // 获取指定位置的元素  
  11.     public Object get(int index) throws Exception;  
  12. }</span>  
设计线性表抽象数据类型的Java的实现

[java]  view plain copy
  1. public class ArrayList implements List {  
  2.     //默认的顺序表的最大长度  
  3.     final int defaultSize = 10;  
  4.     //当前长度  
  5.     int currentSize;  
  6.     //最大长度  
  7.     int maxSize;  
  8.     //对象数组  
  9.     Object[] listArray;  
  10.         //构造方法,默认大小   
  11.     public ArrayList() {  
  12.         init(this.defaultSize);  
  13.     }  
  14.        //构造方法,设置大小  
  15.     public ArrayList(int length) {  
  16.         init(length);  
  17.     }     
  18.     //线性表的初始化方法  
  19.     private void init(int length) {  
  20.         maxSize = length;  
  21.         this.currentSize = 0;  
  22.         listArray = new Object[length];  
  23.     }  
  24.     //ArrayList的大小  
  25.     public int size() {  
  26.         return this.currentSize;  
  27.     }  
  28.     //ArrayList是否为空  
  29.     public boolean isEmpty() {  
  30.         return this.currentSize == 0;  
  31.     }  
  32.     //添加数据  
  33.     public void add(int index, Object obj) throws Exception {  
  34.         if (this.currentSize == this.maxSize) {  
  35.             throw new Exception("线性表已满,无法插入!");  
  36.         }  
  37.         if (index < 0 || index >= this.maxSize) {  
  38.             throw new Exception("参数错误!");  
  39.         }  
  40.         for (int j = this.currentSize; j > index; j--) {  
  41.             listArray[j] = listArray[j - 1];  
  42.         }  
  43.         listArray[index] = obj;  
  44.         this.currentSize++;  
  45.     }  
  46.     //删除数据  
  47.     public void delete(int index) throws Exception {  
  48.         if (this.isEmpty()) {  
  49.             throw new Exception("线性表为空,无法删除!");  
  50.         }  
  51.         if (index < 0 || index > this.maxSize - 1) {  
  52.             throw new Exception("参数错误!");  
  53.         }  
  54.         for (int j = index; j < this.currentSize - 1; j++) {  
  55.             listArray[j] = listArray[j + 1];  
  56.         }  
  57.         this.currentSize--;  
  58.     }  
  59.     //查找指定索引的数据  
  60.     public Object get(int index) throws Exception {  
  61.         if (index < 0 || index >= this.currentSize) {  
  62.             throw new Exception("参数错误!");  
  63.         }  
  64.         return listArray[index];  
  65.     }  
  66. }  
ArrayList效率分析

ArrayList插入和删除一个元素的时间复杂度为O(n)。
ArrayList支持随机访问,ArrayList读取一个元素的时间复杂度为O(1)。

ArrayList的优点是:支持随机访问,空间利用率高。
ArrayList的缺点是:大小固定,插入和删除元素需要移动大量的数据。

测试案例:
[java]  view plain copy
  1. //学生类  
  2. public class Students {  
  3.   
  4.     private String sid;// 学号  
  5.     private String name;// 姓名  
  6.     private String gender;// 性别  
  7.     private int age;// 年龄  
  8.       
  9.     public Students()  
  10.     {  
  11.         super();  
  12.     }  
  13.       
  14.     public Students(String sid,String name,String gender,int age)  
  15.     {  
  16.         this.sid = sid;  
  17.         this.name = name;  
  18.         this.gender = gender;  
  19.         this.age =age;  
  20.     }  
  21.       
  22.     public String toString()  
  23.     {  
  24.        return "学号:"+this.getSid()+" 姓名:"+this.getName()+" 性别:"+this.getGender()+" 年龄:"+this.getAge();     
  25.     }  
  26.       
  27.     public String getSid() {  
  28.         return sid;  
  29.     }  
  30.   
  31.     public void setSid(String sid) {  
  32.         this.sid = sid;  
  33.     }  
  34.   
  35.     public String getName() {  
  36.         return name;  
  37.     }  
  38.   
  39.     public void setName(String name) {  
  40.         this.name = name;  
  41.     }  
  42.   
  43.     public String getGender() {  
  44.         return gender;  
  45.     }  
  46.   
  47.     public void setGender(String gender) {  
  48.         this.gender = gender;  
  49.     }  
  50.   
  51.     public int getAge() {  
  52.         return age;  
  53.     }  
  54.   
  55.     public void setAge(int age) {  
  56.         this.age = age;  
  57.     }  
  58.   
  59. }  
[java]  view plain copy
  1. public class ArrayListTest {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         List list = new ArrayList();  
  8.         try {  
  9.             list.add(0new Students("S0001""张三""男"18));  
  10.             list.add(1new Students("S0002""李四""男"19));  
  11.             list.add(2new Students("S0003""王五""女"21));  
  12.         } catch (Exception e) {  
  13.             // TODO Auto-generated catch block  
  14.             e.printStackTrace();  
  15.         }  
  16.         System.out.println("*************************");  
  17.         for (int i = 0; i < list.size(); i++) {  
  18.             try {  
  19.                 System.out.println(list.get(i).toString());  
  20.             } catch (Exception e) {  
  21.                 // TODO Auto-generated catch block  
  22.                 e.printStackTrace();  
  23.             }  
  24.         }  
  25.         try {  
  26.             list.delete(1);  
  27.         } catch (Exception e) {  
  28.             // TODO Auto-generated catch block  
  29.             e.printStackTrace();  
  30.         }  
  31.         System.out.println("##############################");  
  32.         for (int i = 0; i < list.size(); i++) {  
  33.             try {  
  34.                 System.out.println(list.get(i).toString());  
  35.             } catch (Exception e) {  
  36.                 // TODO Auto-generated catch block  
  37.                 e.printStackTrace();  
  38.             }  
  39.         }  
  40.     }  
  41. }  

测试结果:
*************************
学号:S0001 姓名:张三 性别:男 年龄:18
学号:S0002 姓名:李四 性别:男 年龄:19
学号:S0003 姓名:王五 性别:女 年龄:21
##############################
学号:S0001 姓名:张三 性别:男 年龄:18
学号:S0003 姓名:王五 性别:女 年龄:21

摘自:http://blog.csdn.net/fangfully/article/details/12282693

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值