線性結構定義
(1)除第一個和最後一個數據元素外,每個數據元素只有一個前驅數據元素和一個後繼數據元素;
(2)第一個數據元素沒有前驅數據元素;
(3)最後一個數據元素沒有後繼數據元素。
則稱這樣的數據結構爲線性結構。
線性表抽象數據類型
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效率分析
//學生類
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