以下程序在JDK1.5.0_05環境下調試通過,程序分3個文件,放在同一目錄下
//List.java????? 順序表抽象數據類型的接口定義
public interface List
{
public void insert(int i,Object obj) throws Exception;????? //插入
public Object delete(int i) throws Exception;?????????????? //刪除
public Object getData(int i) throws Exception;????????????? //取數據元素
public int size();????????????????????????????????????????? //求元素個數
public boolean isEmpty();?????????????????????????????????? //是否空
}
//SeqList.java????? 順序表類
public class SeqList implements List
{
final int defaultSize = 10;
int maxSize;
int size;
Object[] listArray;
public SeqList()
{
????? initiate(defaultSize);
}
public SeqList(int size)
{
????? initiate(size);
}
private void initiate(int sz)
{
????? maxSize = sz;
????? size = 0;
????? listArray = new Object[sz];
}
public void insert(int i,Object obj) throws Exception
{
????? if(size == maxSize)
????? {
?????? throw new Exception("順序表已滿無法插入!");
????? }
????? if(i < 0 || i > size)
????? {
?????? throw new Exception("參數錯誤!");
????? }
????? for(int j = size;j > i;j--)
???????? listArray[j] = listArray[j-1];
????? listArray[i] = obj;
????? size++;
}
public Object delete(int i) throws Exception
{
????? if(size == 0)
????? {
?????? throw new Exception("順序表已空無法刪除!");
????? }
????? if(i < 0 || i > size-1)
????? {
?????? throw new Exception("參數錯誤!");
????? }
????? Object it = listArray[i];
????? for(int j = i;j < size-1;j++)
???????? listArray[j] = listArray[j+1];
????? size--;
????? return it;
}
public Object getData(int i) throws Exception
{
????? if(i < 0 || i >= size)
????? {
?????? throw new Exception("參數錯誤!");
????? }
????? return listArray[i];
}
public int size()
{
????? return size;
}
public boolean isEmpty()
{
????? return size == 0;
}
public int MoreDataDelete(SeqList L,Object x) throws Exception
{
????? int i,j;
????? int tag = 0;
????? for(i = 0;i < L.size;i++)
????? {
?????? if(x.equals(L.getData(i)))
?????? {
??????? L.delete(i);
??????? i--;
??????? tag = 1;
?????? }
????? }
????? return tag;
}
}
//SeqListTest1.java????? 示例程序1(主程序)
public class SeqListTest1
{
public static void main(String args[])
{
????? SeqList seqlist = new SeqList(100);
????? int n = 10;
????? try
????? {
?????? for(int i = 0;i < n;i++)
?????? {
??????? seqlist.insert(i,new Integer(i+1));
?????? }
?????? seqlist.delete(4);
?????? for(int i = 0;i < seqlist.size;i++)
?????? {
??????? System.out.print(seqlist.getData(i)+"????? ");
?????? }
????? }
????? catch(Exception e)
????? {
?????? System.out.println(e.getMessage());
????? }
}
}
//SeqListTest2.java???? 示例程序2
public class SeqListTest2
{
public static void main(String args[])
{
??? SeqList seqList = new SeqList(100);
??? Student[] student;
????????? student = new Student[3];
????????? student[0] = new Student(2000001,"張三","男",20);
????????? student[1] = new Student(2000002,"李四","男",21);
????????? student[2] = new Student(2000003,"王五","女",22);
????????? int n = 3;
????????? try
????????? {
???? for(int i = 0;i < n;i++)
???? {
????? seqList.insert(i,student[i]);
???? }
???? for(int i = 0;i < seqList.size;i++)
???? {
????? Student st = (Student)seqList.getData(i);
????? System.out.println(st.getNumber()+"???? "+st.getName()+"???? "+st.getSex()+"???? "+st.getAge());
???? }
??? }
??? catch(Exception e)
??? {
???? System.out.println(e.getMessage());
??? }
}
}
class Student
{
private long number;
private String name;
private String sex;
private int age;
Student(long number,String name,String sex,int age)
{
??? this.number = number;
??? this.name = name;
??? this.sex = sex;
??? this.age = age;
}
public long getNumber()
{
??? return number;
}
public String getName()
{
??? return name;
}
public String getSex()
{
??? return sex;
}
public int getAge()
{
??? return age;
}
}
本站原創,轉帖請注明出處:http://hi.baidu.com/jadmin/blog/謝謝!
posted on 2007-04-29 17:57
jadmin 閱讀(82)
評論(0) 編輯 收藏