java 代码
- package org.danlley.util;
- /**
- * @version 1.0
- * @since 2006 年 9 月 17 日 14 : 42 pm
- * @author weixm
- * 说明:这是一个用数组实现的顺序表,实现了基本的增、删、查操作。
- */
- public class SequenList {
- public int length=10;//初始化顺序表大小为10
- private int cur_index;//当前插入位置
- private Object[] seqList;//顺序表数据存放处
- /**
- * @author weixm
- * 说明:构造函数,用来初始化顺序表。此处用私有方法来对数据进行初始化。
- */
- public SequenList(){
- initList();
- }
- /**
- * @author weixm
- * 初始化顺序表
- */
- private void initList(){
- this.seqList=new Object[length];
- }
- /**
- * @author weixm
- * @param obj
- * 说明:此方法用来在顺序表中最后位置添加新数据,当数据添加位置超过了数组允许大小时,
- * 重新分配内存空间。并且,每加一条数据,List的当前插入位置自增“1”。
- */
- public void add(Object obj){
- if(length>0){
- if(cur_index<(length-1)){
- seqList[cur_index]=obj;
- cur_index++;
- }else{
- resizeList(seqList);
- seqList[cur_index]=obj;
- cur_index++;
- }
- }else{
- System.err.print("The sequences are not initialed properly ! ");
- }
- }
- /**
- * @author weixm
- * @param index
- * @param obj
- * 说明:此方法用来在数组的任意位置添加一个新数据。如果任意插入的数据位置刚好是最后一个元素
- * 则直接调用 add(Object obj) 方法,如果添加的数据超出了原始数据的边界值,则List
- * 会自动对空间进行扩充。因此,允许添加数据的当前位置超出数据边界。但是每次插入的数据
- * 索引大小不能超过 length*3/2+1 ,否则,系统会抛出数组越界异常。
- */
- public void add(int index,Object obj){
- if(index==cur_index+1){
- add(obj);
- }else{
- int old_len=length;
- if(index>length-1){
- resizeList(seqList);
- }
- System.arraycopy(seqList,index,seqList,index+1,old_len-index);
- seqList[index]=obj;
- cur_index=index;
- }
- }
- /**
- * @author weixm
- * @param obj
- * @return
- * 说明:此方法用于进行元素查找,如果要查找的元素在数据中存在,则返回数据所在位置,如果
- * 元素在数据中没有找到,则直接返回 -1 。
- */
- public int findElement(Object obj){
- int find_index=-1;
- for(int i=0;i
- if(obj.equals(seqList[i])){
- find_index=i;
- }
- }
- return find_index;
- }
- /**
- * @author weixm
- * @param index
- * @return
- * 说明:此方法用来得到某个确定位置的元素。
- */
- public Object get(int index){
- return seqList[index];
- }
- /**
- * @author weixm
- * @param seqList_low
- * 说明:重新划分并扩充数据所占内存空间,具体扩充方式为: length*3/2+1 。
- */
- public void resizeList(Object[] seqList_low){
- int resize=length*3/2+1;
- Object[] seqList_upp=new Object[resize];
- System.arraycopy(seqList_low,0,seqList_upp,0,length);
- seqList=seqList_upp;
- length=resize;
- }
- /**
- * @author weixm
- * @param args
- * 说明:测试用例
- */
- // public static void main(String[] args)throws Exception{
- // try{
- // SequenList list=new SequenList();
- // for(int i=0;i<100;i++){
- // list.add(""+i);
- // //System.out.println(" "+list.get(i));
- // }
- // list.add(133," TEST ");
- // System.out.println(""+list.findElement(""+98));
- // System.out.println(""+list.findElement(" TEST "));
- // System.out.println(""+list.findElement("TEST"));
- // for(int i=0;i
- // System.out.println(" "+list.get(i));
- // }
- // System.out.println("++++++++++++ "+list.length);
- // }catch(Exception e){
- // throw new Exception(e);
- // }
- // }
- }