---
title: "用Java实现的顺序链表"
author: "danlley"
date: "2007-07-22T11:59:00Z"
tags: []
slug: "java"
draft: false
lastmod: "2007-07-22T11:59:00Z"
id: 101948
url: "https://danlley.iteye.com/blog/101948"
---

java 代码

001. package org.danlley.util;
002. /\**
003. * @version 1.0
004. * @since 2006 年 9 月 17 日     14 : 42 pm
005. * @author weixm
006. * 说明：这是一个用数组实现的顺序表，实现了基本的增、删、查操作。
007. \*/
008. public class SequenList {
009. public int length=10;//初始化顺序表大小为10
010. private int cur\_index;//当前插入位置
011. private Object\[] seqList;//顺序表数据存放处
012. /\**
013. * @author weixm
014. * 说明：构造函数，用来初始化顺序表。此处用私有方法来对数据进行初始化。
015. \*/
016. public SequenList(){
017. initList();
018. }
019. /\**
020. * @author weixm
021. * 初始化顺序表
022. \*/
023. private void initList(){
024. this.seqList=new Object\[length];
025. }
026. /\**
027. * @author weixm
028. * @param obj
029. * 说明：此方法用来在顺序表中最后位置添加新数据，当数据添加位置超过了数组允许大小时，
030. *      重新分配内存空间。并且，每加一条数据，List的当前插入位置自增“1”。
031. \*/
032. public void add(Object obj){
033. if(length&gt;0){
034. if(cur\_index&lt;(length-1)){
035. seqList\[cur\_index]=obj;
036. cur\_index++;
037. }else{
038. resizeList(seqList);
039. seqList\[cur\_index]=obj;
040. cur\_index++;
041. }
042. }else{
043. System.err.print("The sequences are not initialed properly ! ");
044. }
045. }
046. /\**
047. * @author weixm
048. * @param index
049. * @param obj
050. * 说明：此方法用来在数组的任意位置添加一个新数据。如果任意插入的数据位置刚好是最后一个元素
051. *      则直接调用 add(Object obj) 方法，如果添加的数据超出了原始数据的边界值，则List
052. *      会自动对空间进行扩充。因此，允许添加数据的当前位置超出数据边界。但是每次插入的数据
053. *      索引大小不能超过 length\*3/2+1 ，否则，系统会抛出数组越界异常。
054. \*/
055. public void add(int index,Object obj){
056. if(index==cur\_index+1){
057. add(obj);
058. }else{
059. int old\_len=length;
060. if(index&gt;length-1){
061. resizeList(seqList);
062. }
063. System.arraycopy(seqList,index,seqList,index+1,old\_len-index);
064. seqList\[index]=obj;
065. cur\_index=index;
066. }
067. }
068. /\**
069. * @author weixm
070. * @param obj
071. * @return
072. * 说明：此方法用于进行元素查找，如果要查找的元素在数据中存在，则返回数据所在位置，如果
073. *      元素在数据中没有找到，则直接返回 -1 。
074. \*/
075. public int findElement(Object obj){
076. int find\_index=-1;
077. for(int i=0;i
078. if(obj.equals(seqList\[i])){
079. find\_index=i;
080. }
081. }
082. return find\_index;
083. }
084. /\**
085. * @author weixm
086. * @param index
087. * @return
088. * 说明：此方法用来得到某个确定位置的元素。
089. \*/
090. public Object get(int index){
091. return seqList\[index];
092. }
093. /\**
094. * @author weixm
095. * @param seqList\_low
096. * 说明：重新划分并扩充数据所占内存空间，具体扩充方式为： length\*3/2+1 。
097. \*/
098. public void resizeList(Object\[] seqList\_low){
099. int resize=length\*3/2+1;
100. Object\[] seqList\_upp=new Object\[resize];
101. System.arraycopy(seqList\_low,0,seqList\_upp,0,length);
102. seqList=seqList\_upp;
103. length=resize;
104. }
105. /\**
106. * @author weixm
107. * @param args
108. * 说明：测试用例
109. \*/
110. // public static void main(String\[] args)throws Exception{
111. //  try{
112. //   SequenList list=new SequenList();
113. //   for(int i=0;i&lt;100;i++){
114. //    list.add(""+i);
115. //    //System.out.println("        "+list.get(i));
116. //   }
117. //   list.add(133," TEST ");
118. //   System.out.println(""+list.findElement(""+98));
119. //   System.out.println(""+list.findElement(" TEST "));
120. //   System.out.println(""+list.findElement("TEST"));
121. //   for(int i=0;i
122. //    System.out.println("        "+list.get(i));
123. //   }
124. //   System.out.println("++++++++++++ "+list.length);
125. //  }catch(Exception e){
126. //   throw new Exception(e);
127. //  }
128. // }
129. }