1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
public class SequenceList { private int[] elem; private int usedSize;
public SequenceList() { this.elem = new int[1]; }
public SequenceList(int size) { this.elem = new int[size]; }
public void addSize(){ this.elem = Arrays.copyOf(this.elem,this.elem.length*2); }
public boolean isFull(){ if(this.elem.length==usedSize) return true; return false; }
public void printList(){ for (int i = 0; i < usedSize; i++) { System.out.print(elem[i]+" "); } }
public void insert(int i,int e){ if(isFull()){ addSize(); } if(i>usedSize){ System.out.println("false"); } for (int j = usedSize-1; j > i-1 ; j--) { elem[j+1] = elem[j]; } elem[i] = e; this.usedSize++; } public void insert(int e){ if(isFull()){ addSize(); } elem[usedSize] = e; this.usedSize++; }
public boolean contain(int e){ for (int i = 0; i < usedSize; i++) { if(elem[i]==e) return true; } return false; }
public int location(int e){ for (int i = 0; i < usedSize; i++) { if(elem[i] == e) return i; } return -1; }
public int getPosElem(int pos){ if(pos<0 || pos>usedSize){ System.out.println("输入位置不合法"); return -1; } return elem[pos]; }
public boolean editElem(int pos,int e){ if(pos<0 || pos>usedSize){ System.out.println("输入位置不合法"); return false; } elem[pos] = e; return true; }
public boolean del(int pos){ if(pos<0 || pos>usedSize){ System.out.println("输入位置不合法"); return false; } for (int i = pos; i < usedSize-1; i++) { elem[i] = elem[i+1]; } usedSize--; return true; } }
|