一、顺序表
顺序表属于线性表的一种,一般以数组形式实现,其作为最基本的数据结构,对于掌握数据结构的基本思想具有重要意义。
二、代码结构
1.IList泛型接口:给出了MyArrayList中各种方法,供MyArrayList泛型类应用。
2.MyArrayList泛型类:应用IList泛型接口并重写相关方法。
3.PosilegalException异常类:定义了位置非法异常,供MyArrayList重写相关方法时判断位置参数非法并抛出异常使用。
4.Test测试类:在其main方法中实例化MyArrayList并且演示顺序表各种方法的最终效果。
三、程序源码
1.PosilegalException异常类
public class PosilegalException extends RuntimeException{
//无参构造方法
public PosilegalException(){
super();
}
//有参构造方法
public PosilegalException(String msg){
super(msg);
}
值得注意的是,PosilegalException应当为运行时异常,所以继承的父类为RuntimeException。
2.IList泛型接口
public interface IList<T> {
//在数组最后新增元素
void add(T data);
//上一个功能的重载:在指定位置新增元素
void add(int pos,T data);
//删除指定元素
void remove (T toRemove);
//查找是否包含某元素
boolean contains(T toFind);
//查找某元素并返回下标
int indexOf(T toFind);
//设定指定下标位置元素
void set(int pos,T data);
//获取指定下标位置元素
T get(int pos);
//打印整个顺序表
void display();
//清空整个顺序表
void clear();
}
接口中提供方法如上图。
3.MyArrayList泛型类
(1)泛型类定义
public class MyArrayList<T> implements IList<T>
泛型类应当标注相同类型参数T并且应用IList接口。
(2)成员变量
//数组成员
public T[] elem;
//usedSize记录已有元素个数
public int usedSize;
(3)构造方法
public MyArrayList(){
// 若写为"elem= new T[10]"编译器会报错
// 原因是编译器初始化对象时必须指定类型
// 因此可以定义为Object类数组再强转为泛型
elem=(T[])new Object[10];
}
(4)判满、判断pos参数是否合法相关方法
//判满方法
private boolean isFull(){
return usedSize== elem.length;
}
//判断添加元素时,pos是否合法的方法
private void checkPosOfAdd(int pos){
if(pos<0||pos>usedSize){
//不合法则抛出异常
throw new PosilegalException("添加元素pos位置不合法!");
}
}
//判断设定元素时,pos是否合法的方法
private void checkPosOfSet(int pos){
if(pos<0||pos>=usedSize) {
//不合法则抛出异常
throw new PosilegalException("设定/获取元素pos位置不合法!");
}
}
(5)顺序表中各种方法:增、删、查找、设定、展示、清空相关方法
//在数组最后新增元素
public void add(T data){
if(isFull()){
//如果判满则调用Arrays的扩容方法
Arrays.copyOf(elem,2*usedSize);
}
elem[usedSize]=data;
usedSize++;
}
//上一个功能的重载:在指定位置新增元素
public void add(int pos,T data){
//利用try,catch处理异常
try {
//判断pos是否合法
checkPosOfAdd(pos);
}catch (PosilegalException e){
e.printStackTrace();
}
if(isFull()){
//如果判满则调用Arrays的扩容方法
Arrays.copyOf(elem,2*usedSize);
}
//从usedSize-1位置到pos位置从左到右移动元素
for(int i=usedSize;i>=pos;i--){
elem[i+1]=elem[i];
}
//插入元素
elem[pos]=data;
usedSize++;
}
//删除指定元素
public void remove (T toRemove){
//调用indexOf方法判断是否有所删除元素
int pos=indexOf(toRemove);
if(pos==-1){
System.out.println("没有要删除的元素!");
return;
}
//从pos位置到usedSize位置从右到左移动元素
for(int i=pos;i<usedSize-1;i++){
elem[i]=elem[i+1];
}
usedSize--;
}
//查找是否包含某元素
public boolean contains(T toFind) {
for (int i = 0; i < usedSize; i++) {
//调用元素的equals方法
if (elem[i].equals(toFind)) {
return true;
}
}
return false;
}
//查找某元素并返回下标
public int indexOf(T toFind){
for (int i = 0; i < usedSize; i++) {
//调用元素的equals方法
if (elem[i].equals(toFind)) {
return i;
}
}
//找不到则返回-1
return -1;
}
//设定指定下标位置元素
public void set(int pos,T data){
try {
//判断pos是否合法
checkPosOfAdd(pos);
}catch (PosilegalException e){
e.printStackTrace();
}
elem[pos]=data;
}
//获取指定下标位置元素
public T get(int pos){
try {
//判断pos是否合法
checkPosOfAdd(pos);
}catch (PosilegalException e){
e.printStackTrace();
}
return elem[pos];
}
//打印整个顺序表
public void display(){
for(int i=0;i<usedSize;i++){
//调用元素的toString方法
System.out.print(elem[i].toString()+" ");
}
System.out.println();
}
//清空整个顺序表
public void clear(){
for(int i=0;i<usedSize;i++)
{
//所有元素引用置空
elem[i]=null;
}
usedSize=0;
}
四、Test示例演示
以下为MyArrayList实例化时将包装类Double作为类型参数的示例。
1.示例一
public class Test {
public static void main(String[] args) {
MyArrayList<Double> myArrayList=new MyArrayList<Double>();
Double element1 =1.0;
Double element2 =2.0;
Double element3 =3.0;
Double element4 =4.0;
//增添元素示例
myArrayList.add(element1);
myArrayList.add(element2);
myArrayList.add(element3);
myArrayList.display();
System.out.println("*************");
//删除元素示例
myArrayList.remove(1.0);
myArrayList.display();
System.out.println("*************");
//查找元素示例
if(myArrayList.contains(2.0)){
System.out.println("找到2.0了");
}
System.out.println("它的下标是"+myArrayList.indexOf(2.0));
System.out.println("*************");
//设定元素示例
myArrayList.set(1,element4);
myArrayList.display();
System.out.println("*************");
//获取元素示例
System.out.println("下标为1的元素为"+myArrayList.get(1));
System.out.println("*************");
//清空顺序表示例
myArrayList.clear();
myArrayList.display();
}
}
运行结果如下:
2.示例二
public class Test {
public static void main(String[] args) {
MyArrayList<Double> myArrayList=new MyArrayList<Double>();
Double element1 =1.0;
Double element2 =2.0;
Double element3 =3.0;
Double element4 =4.0;
//添加元素不合法案例
myArrayList.add(1000,element4);
}
}
运行结果如下:
五、小结
以上便是通过Java实现数据结构——泛型顺序表,实现增、删、查、设定与获取值、打印、清空操作的全部内容,如有不当,敬请斧正。