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

来源:互联网 发布:go编程语言 编辑:程序博客网 时间:2024/05/01 05:39

线性结构定义

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

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

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

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

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

线性表抽象数据类型

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

1.求元素个数

2.插入

3.删除

4.查找

5.判断是否为空

5.判断是否为空

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

public interface List {// 获得线性表长度public int size();// 判断线性表是否为空public boolean isEmpty();// 插入元素public void add(int index, Object obj) throws Exception;// 删除元素public void delete(int index) throws Exception;// 获取指定位置的元素public Object get(int index) throws Exception;}
设计线性表抽象数据类型的Java的实现

public class ArrayList implements List {//默认的顺序表的最大长度final int defaultSize = 10;//当前长度int currentSize;//最大长度int maxSize;//对象数组Object[] listArray;        //构造方法,默认大小public ArrayList() {init(this.defaultSize);}       //构造方法,设置大小public ArrayList(int length) {init(length);}//线性表的初始化方法private void init(int length) {maxSize = length;this.currentSize = 0;listArray = new Object[length];}//ArrayList的大小public int size() {return this.currentSize;}//ArrayList是否为空public boolean isEmpty() {return this.currentSize == 0;}//添加数据public void add(int index, Object obj) throws Exception {if (this.currentSize == this.maxSize) {throw new Exception("线性表已满,无法插入!");}if (index < 0 || index >= this.maxSize) {throw new Exception("参数错误!");}for (int j = this.currentSize; j > index; j--) {listArray[j] = listArray[j - 1];}listArray[index] = obj;this.currentSize++;}//删除数据public void delete(int index) throws Exception {if (this.isEmpty()) {throw new Exception("线性表为空,无法删除!");}if (index < 0 || index > this.maxSize - 1) {throw new Exception("参数错误!");}for (int j = index; j < this.currentSize - 1; j++) {listArray[j] = listArray[j + 1];}this.currentSize--;}//查找指定索引的数据public Object get(int index) throws Exception {if (index < 0 || index >= this.currentSize) {throw new Exception("参数错误!");}return listArray[index];}}
ArrayList效率分析

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

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

测试案例:
//学生类public class Students {private String sid;// 学号private String name;// 姓名private String gender;// 性别private int age;// 年龄    public Students(){super();}public Students(String sid,String name,String gender,int age){this.sid = sid;this.name = name;this.gender = gender;this.age =age;}public String toString(){   return "学号:"+this.getSid()+" 姓名:"+this.getName()+" 性别:"+this.getGender()+" 年龄:"+this.getAge();}public String getSid() {return sid;}public void setSid(String sid) {this.sid = sid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
public class ArrayListTest {/** * @param args */public static void main(String[] args) {List list = new ArrayList();try {list.add(0, new Students("S0001", "张三", "男", 18));list.add(1, new Students("S0002", "李四", "男", 19));list.add(2, new Students("S0003", "王五", "女", 21));} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("*************************");for (int i = 0; i < list.size(); i++) {try {System.out.println(list.get(i).toString());} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}try {list.delete(1);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("##############################");for (int i = 0; i < list.size(); i++) {try {System.out.println(list.get(i).toString());} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

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