Résumé: 1 table de séquence est le bon tableau familier accès aléatoire,
2. un endroit donné peut efficacement acquérir / modifier la valeur de la complexité de l' emplacement spécifié est en O (1)
3. commande par table de valeur recherche, insertion , la suppression, la complexité du temps de O (N)
4. pour la queue et queue supprimés interpolation, la complexité en temps de O (1)
//import java.util.Arrays;
public class SeqList {
private int[] data = new int[10];
private int size = 0;
//打印顺序表
public void display() {
//System.out.println(Arrays.toString(data));
System.out.print("[");
for (int i = 0; i < size; i++) {
System.out.print(data[i]);
if(i != size - 1) {
System.out.print(",");
}
}
System.out.println("]");
}
//在pos位置新增元素
public void add(int pos,int elem) {
if (pos < 0 || pos > size) {
return;
}
if(this.size >= this.data.length) {
realloc();//扩容
}
if(pos == size) {
this.data[pos] = elem;
size++;
return;
}
for(int i = this.size;i > pos;i--) {
this.data[i] = this.data[i-1];
}
this.data[pos] = elem;
this.size++;
}
private void realloc() {
int[] newData = new int[this.data.length * 2];
for (int i = 0; i < this.data.length; i++) {
newData[i] = this.data[i];
}
this.data = newData;
}
//判定是否包含某个元素
public boolean contains(int toFind) {
for (int i = 0; i < this.size; i++) {
if (this.data[i] == toFind) {
return true;
}
}
return false;
}
//return this.search(toFind) != -1;
//查找某个元素对应的位置
public int search(int toFind) {
for (int i = 0; i < this.size; i++) {
if(this.data[i] == toFind) {
return i;
}
}
return -1;
}
//获取pos位置的元素
public int getPos(int pos) {
return this.data[pos];
}
//给pos位置的元素设为elem
public void setPos(int pos,int elem) {
this.data[pos] = elem;
}
//删除第一次出现的关键字key
public void remove(int toRemove) {
int pos = search(toRemove);
if(pos == -1) {
return;
}
if(pos == this.size - 1) {
this.size--;
return;
}
for (int i = pos; i < size - 1; i++) {
this.data[i] = this.data[i + 1];
}
this.size--;
}
//获取顺序表的长度
public int size () {
return this.size;
}
//清空顺序表
public void clear() {
this.size = 0;
this.data = new int[10];
}
}
tests unitaires
public class Test {
public static void main(String[] args) {
testdisplay();
testadd();
testcontains();
testsearch();
testgetPos();
testsetPos();
testremove();
}
public static void testdisplay(){
System.out.println("测试打印:");
SeqList seqList = new SeqList();
seqList.display();
}
public static void testadd() {
System.out.println("测试 add 方法:");
SeqList seqList = new SeqList();
seqList.add(0,1);
seqList.add(1,2);
seqList.add(2,3);
seqList.add(3,4);
seqList.add(2,5);
seqList.display();
}
public static void testcontains() {
System.out.println("测试 contains 方法:");
SeqList seqList = new SeqList();
seqList.add(0,1);
seqList.add(1,2);
seqList.add(2,3);
seqList.add(3,4);
seqList.add(2,5);
boolean result = seqList.contains(5);
System.out.println("result预期是 true;实际是 " +result);
}
public static void testsearch() {
System.out.println("测试 search 方法:");
SeqList seqList = new SeqList();
seqList.add(0,1);
seqList.add(1,2);
seqList.add(2,3);
seqList.add(3,4);
seqList.add(2,5);
int result = seqList.search(5);
System.out.println("result预期是 2;实际是 " + result);
}
public static void testgetPos() {
System.out.println("测试 getPos 方法:");
SeqList seqList = new SeqList();
seqList.add(0,1);
seqList.add(1,2);
seqList.add(2,3);
seqList.add(3,4);
int result = seqList.getPos(3);
System.out.println("result预期是 4;实际是 "+result);
}
public static void testsetPos() {
System.out.println("测试 setPos 方法:");
SeqList seqList = new SeqList();
seqList.add(0,1);
seqList.add(1,2);
seqList.add(2,3);
seqList.add(3,4);
seqList.setPos(2,5);
System.out.println("预期值:[1,2,5,4]");
System.out.print("实际值:");
seqList.display();
}
public static void testremove() {
System.out.println("测试 remove 方法:");
SeqList seqList = new SeqList();
seqList.add(0,1);
seqList.add(1,2);
seqList.add(2,3);
seqList.add(3,4);
seqList.remove(2);
System.out.println("预期值:[1,3,4]");
System.out.print("实际值:");
seqList.display();
}
}