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

线性结构定义

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

(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 block
			e.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 block
				e.printStackTrace();
			}
		}
		try {
			list.delete(1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.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 block
				e.printStackTrace();
			}
		}
	}
}

测试结果:
*************************
学号:S0001 姓名:张三 性别:男 年龄:18
学号:S0002 姓名:李四 性别:男 年龄:19
学号:S0003 姓名:王五 性别:女 年龄:21
##############################
学号:S0001 姓名:张三 性别:男 年龄:18
学号:S0003 姓名:王五 性别:女 年龄:21
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基于数组实现线性结构List接口的实现类(Java语言): ``` public class ArrayList<E> implements List<E> { private E[] array; // 存储元素的数组 private int size; // 当前元素个数 // 构造函数:初始化数组大小为10 public ArrayList() { this(10); } // 构造函数:初始化数组大小为initialCapacity public ArrayList(int initialCapacity) { if (initialCapacity < 0) { throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); } array = (E[]) new Object[initialCapacity]; } // 添加元素到指定位置 @Override public void add(int index, E element) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); } if (size == array.length) { ensureCapacity(size + 1); } System.arraycopy(array, index, array, index + 1, size - index); array[index] = element; size++; } // 添加元素到末尾 @Override public boolean add(E element) { if (size == array.length) { ensureCapacity(size + 1); } array[size++] = element; return true; } // 删除指定位置的元素 @Override public E remove(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); } E oldValue = array[index]; int numMoved = size - index - 1; if (numMoved > 0) { System.arraycopy(array, index + 1, array, index, numMoved); } array[--size] = null; // 将最后一个元素设为null,便于垃圾回收 return oldValue; } // 获取指定位置的元素 @Override public E get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); } return array[index]; } // 设置指定位置的元素 @Override public E set(int index, E element) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); } E oldValue = array[index]; array[index] = element; return oldValue; } // 返回元素个数 @Override public int size() { return size; } // 判断是否为空 @Override public boolean isEmpty() { return size == 0; } // 扩容数组 private void ensureCapacity(int minCapacity) { int oldCapacity = array.length; if (minCapacity > oldCapacity) { E[] oldArray = array; int newCapacity = (oldCapacity * 3) / 2 + 1; if (newCapacity < minCapacity) { newCapacity = minCapacity; } array = (E[]) new Object[newCapacity]; System.arraycopy(oldArray, 0, array, 0, size); } } } ``` 这个实现类使用了泛型,可以存储任意类型的元素。其中,add() 方法有两个重载,一个是添加元素到指定位置,一个是添加元素到末尾;remove() 方法删除指定位置的元素;get() 方法获取指定位置的元素;set() 方法设置指定位置的元素;size() 方法返回元素个数;isEmpty() 方法判断是否为空;ensureCapacity() 方法扩容数组。注意,数组下标从0开始。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值