顺序表的Java实现
线性表
线性表是一种相当简单灵活的数据结构,是我们学习数据结构的基础。线性表中的元素存在一对一的关系。例如,将学生按学号进行先后顺序的排列。
线性表可以用顺序存储和链式存储,顺序存储适合进行多次访问,而链式存储适合多次插入和删除。
顺序表
顺序储存结构就是借助元素在存储器中的相对位置来表示数据元素之间的逻辑关系,通常借助程序设计语言的数组类型来描述。
数据元素
新建一个类来封装数据元素
包含两个数据项,并用get()、set()方法进行访问和传值。
ElemType |
int number |
String name |
public class ElemType {
private String name;//随便定义两个数据项
private int number;
public ElemType(){ }//建立两个构造方法便于有参和无惨创建
public ElemType(String name,int number){
this.name=name;
this.number=number;
}
/*-----get()set()方法-----*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
/*-----toString方法,便于后续打印输出-----*/
public String toString(){
return this.getName()+" "+this.getNumber();
}
}
抽象数据类型(ADT)
为了方便以后对链表的学习,可以定义一个抽象数据类型的接口,抽象出线性表的一些基本方法。在顺序表和链表中实现这些方法。
public interface MyList {
/*清空线性表*/
public void clear();
/*返回线性表是否为空*/
public boolean isEmpty();
/*返回线性表的长度*/
public int listLength();
/*返回指定第i个元素*/
public ElemType getElem(int i) throws Exception;
/*返回指点元素的位置*/
public int getIndex(ElemType x) throws Exception;
/*在线性表第i个元素前插入x*/
public void insert(int i, ElemType x) throws Exception;
/*删除线性表中的第i个数据元素*/
public void remove(int i) throws Exception;
/*输出线性表中的数据元素 */
public void display();
}
顺序表SqList
public class SqList implements MyList {
private int maxLength;//最大长度
private int curLength;//当前长度
private ElemType[] listElem;//存储数据元素的空间
public SqList(int maxSize) {//构造方法,以参数maxSize作为顺序表的最大容量
this.maxLength = maxSize;
listElem = new ElemType[maxSize];
}
@Override
public void clear() {
curLength = 0;//我们把小于当前长度的元素看做有效元素,因此,清理时我们只需将curLength置为0即可,其他位置上的值为多少对我们而言没有意义。
}
@Override
public boolean isEmpty() {
return curLength == 0;
}
@Override
public int listLength() {
return curLength;
}
@Override
public ElemType getElem(int i) throws Exception {
if (i < 0 || i > curLength) {
throw new Exception("第" + i + "个元素不存在");
}
return listElem[i];
}
@Override
public int getIndex(ElemType x) throws Exception {
for (int i = 0; i < curLength; i++) {
if (listElem[i].toString().equals(x.toString())) {
return i;
}
}
throw new Exception("不存在你要查找的元素");
}
@Override
public void insert(int i, ElemType x) throws Exception {
if (curLength == maxLength) // 判断顺序表是否已满
throw new Exception("顺序表已满");// 输出异常
if (i < 0 || i > curLength) // i小于0或者大于表长
throw new Exception("插入位置不合理");// 输出异常
for (int j = curLength - 1; j >= i; j--) {
listElem[j + 1] = listElem[j];
}
listElem[i] = x;
curLength++;//当前长度必须增加1
}
@Override
public void remove(int i) throws Exception {
if (i < 0 || i > curLength - 1) // i小于0或者大于表长
throw new Exception("删除位置不合理");// 输出异常
for (int j = i; j < curLength - 1; j--) {
listElem[j] = listElem[j + 1];
}
curLength--;//当前长度必须减少1
}
@Override
public void display() {
for (int i = 0; i < curLength; i++) {
System.out.print(listElem[i].toString());
if (i != curLength - 1) {
System.out.print(" -> ");
}
}
System.out.println();
}
}
测试代码
新建一个测试类,用于测试代码(包括以后要学习的其他基础数据结构)
public class ListTest {
public static void main(String[] args){
System.out.println("/---创建顺序表:---/");
SqList sqList=new SqList(5);
ElemType elem1=new ElemType("王",1);
ElemType elem2=new ElemType("张",2);
ElemType elem3=new ElemType("李",3);
ElemType elem4=new ElemType("李",4);
ElemType elem5=new ElemType("赵",5);
System.out.println("是否为空:"+sqList.isEmpty());
System.out.println("输出顺序表:");
sqList.display();
System.out.println();
System.out.println("/---插入操作:---/");
try {
sqList.insert(0,elem1);
sqList.insert(1,elem2);
sqList.insert(2,elem3);
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("是否为空:"+sqList.isEmpty());
System.out.println("输出顺序表:");
sqList.display();
System.out.println();
System.out.println("/---插入操作:---/");
try {
sqList.insert(1,elem4);
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("输出顺序表:");
sqList.display();
System.out.println();
System.out.println("/---查找索引:---/");
try {
System.out.println(elem4.getName()+" "+elem4.getNumber()+"的下标为:"+sqList.getIndex(elem4));
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
测试结果