顺序表定义:
顺序表是一种顺序存储的线性结构。比如A0 … Ai … An-1这样的表,表中的第一个元素是A0,表的最后一个元素是An-1,A0没有前驱元,An-1没有后继元,元素Ai在表中的位置是i+1,表中逻辑上相邻的两个元素在物理位置上也相邻。
下面是我写的一个小demo
定义接口
public interface IList {
public int length();//获取顺序表的size
public boolean isEmpty();//判断顺序表是否为空
public void clear();//将顺序表置空
public Object get(int i) throws Exception;//获取顺序表中获取索引为i的元素并返回
public void insert(int i,Object o) throws Exception;//在顺序表索引i之前插入元素o
public void remove(int i) throws Exception;//删除线性表中索引为i的元素
public int indexOf(Object o);//返回首次出现指定元素的索引
}
实现类
import java.util.Arrays;
public class SequenceList implements IList{
private Object[] sList;
private int tab_len;
//定义默认容量
private static final int DEFAULT_SIZE=10;
public SequenceList(){
tab_len=0;
sList=new Object[DEFAULT_SIZE];
}
public SequenceList(int initSize){
tab_len=0;
sList=new Object[initSize];
}
@Override
public int length() {
return tab_len;
}
@Override
public boolean isEmpty() {
return tab_len==0;
}
@Override
public void clear() {
tab_len=0;
}
@Override
public Object get(int i) throws Exception {
if(i<0 || i>tab_len-1){
throw new Exception("索引异常,当前索引的元素不存在");
}
return sList[i];
}
@Override
public void insert(int i, Object o) throws Exception {
if(i<0||i>tab_len){
throw new Exception("索引异常,错误插入");
}
if(tab_len==sList.length){
//扩容为原有容量的1.5倍
int newCapacity;
if(tab_len+(tab_len>>1)-Integer.MAX_VALUE>0){
newCapacity=Integer.MAX_VALUE;
}else {
newCapacity = tab_len + (tab_len >> 1);
}
sList = Arrays.copyOf(sList, newCapacity);
}
// for(int j=tab_len;j>i;j--){
// sList[j]=sList[j-1];
// }
System.arraycopy(sList, i, sList, i + 1, tab_len - i);
sList[i]=o;
tab_len++;
}
@Override
public void remove(int i) throws Exception {
if(i<0||i>tab_len-1){
throw new Exception("索引异常,你要删除的元素不存在");
}
// for(int j=i;j<tab_len-1;j++){
// sList[j]=sList[j+1];
// }
System.arraycopy(sList, i + 1, sList, i, tab_len - 1 - i);
tab_len--;
}
@Override
public int indexOf(Object o) {
int i;
for(i=0;i<=tab_len;i++){
if(i==tab_len || sList[i].equals(o)){
break;
}
}
return i==tab_len? -1:i;
}
}
测试
public static void main(String[] args) throws Exception {
SequenceList sList=new SequenceList(3);
sList.insert(0,"张三");
sList.insert(1,"李四");
sList.insert(2,'a');
sList.insert(3,100);
sList.insert(4,'a');
sList.insert(2,300);
System.out.println(sList.isEmpty());
int i3 = sList.indexOf('a');
System.out.println("\'a'的索引为"+i3);
System.out.println("表的元素个数"+sList.length());
sList.remove(1);
System.out.println("表的元素个数"+sList.length());
for(int i=0;i<sList.length();i++){
System.out.println(sList.get(i));
}
}
测试结果
false
'a'的索引为3
表的元素个数6
表的元素个数5
张三
300
a
100
a
附
arraycopy()方法参数介绍
public static native void arraycopy(Object src, int srcPos,Object dest, int destPos,int length);
- Object src:原数组
- int srcPos:原数组复制的起始位置
- Object dest:目的数组
- Object destPos:要复制到目的数组的起始位置
- int length:复制的长度
测试小demo
public static void main(String[] args) {
String[] srcArray={"1","2","3","4"};
String[] destArray={"5","6","7","8"};
//将srcArray数组中的索引1后面的两个元素复制到destArray数组中索引2后面
System.arraycopy(srcArray,1,destArray,2,2);
for (String str :destArray){
System.out.print(str+" ");
}
}
测试结果
5 6 2 3
结语
不积跬步,无以至千里。充实自己,只为更好的自己。