package com.java.datastructure.lineartable;
// 线性表 - java数组实现方式
public class ArrayLinkList {
private Object[] data;
private int length;
private int MAXSIZE;
public ArrayLinkList(int MAXSIZE) {
this.MAXSIZE = MAXSIZE;
data = new Object[MAXSIZE];
}
public Boolean hasElem(int i) {
if(this.length==0 || i<1 || i>this.length) {
return false;
} else {
return true;
}
}
public Object getElem(int i) {
if(this.length==0 || i<1 || i>this.length) {
return null;
} else {
return data[i-1];
}
}
public boolean insertElem(Object obj, int i) {
int k;
//线性表长度不够
if(length==this.MAXSIZE) {
return false;
}
//插入位置不在范围内
if(i<1 || i>(length+1)) {
return false;
}
//判断是插入元素不在表尾
if(i<=length) {
//将要插入位置后数据元素向后移动一位
for (k = length-1; k>=i-1; k--) {
data[k+1] = data[k];
}
}
data[i-1] = obj;
length++;
return true;
}
public static void main (String [] args) {
ArrayLinkList table = new ArrayLinkList(50);
table.insertElem("one-obj",1);
table.insertElem("two-obj",1);
System.out.println("获取第一个元素值:--"+table.getElem(1));
System.out.println("获取第二个元素值:--"+table.getElem(2));
}
}