线性表
线性表是按顺序存储数据时常用的一种数据结构。实现方式有两种:一种是使用数组,另一种是使用链式结构
接口MyList.java
1 public interface MyList<E>{ 2 public void add(E e); 3 4 public void add(int index, E e); 5 6 public void clear(); 7 8 public boolean contains(E e); 9 10 public E get(int index); 11 12 public int indexOf(E e); 13 14 public int lastIndexOf(E e); 15 16 public boolean isEmpty(); 17 18 public boolean remove(E e); 19 20 public E remove(int index); 21 22 public Object set(int index, E e); 23 24 public int size(); 25 }
实现接口MyList部分方法的抽象类MyAbstractList.java
1 public abstract class MyAbstractList<E> implements MyList<E> { 2 protected int size = 0; 3 4 protected MyAbstractList(){ 5 6 } 7 protected MyAbstractList(E[] objects){ 8 for(int i = 0; i < objects.length; i++){ 9 add(objects[i]); 10 } 11 } 12 public void add(E e){ 13 add(size, e); 14 } 15 public boolean isEmpty(){ 16 return size == 0; 17 } 18 public int size(){ 19 return size; 20 } 21 public boolean remove(E e){ 22 if(indexOf(e) >= 0){ 23 remove(indexOf(e)); 24 return true; 25 } 26 else { 27 return false; 28 } 29 } 30 }
用数组实现线性表MyArrayList.java
1 public class MyArrayList<E> extends MyAbstractList<E> { 2 public static final int INITIAL_CAPACITY = 16; 3 private E[] data = (E[])new Object[INITIAL_CAPACITY]; 4 5 public MyArrayList(){ 6 7 } 8 public MyArrayList(E[] objects){ 9 for(int i = 1; i < objects.length; i++){ 10 add(objects[i]); 11 } 12 } 13 public void add(int index, E e){ 14 ensureCapacity(); 15 for(int i = size -1; i >= index; i--){ 16 data[i+1] = data[i]; 17 } 18 data[index] = e; 19 20 size++; 21 } 22 public void ensureCapacity(){ 23 if(size >= data.length){ 24 E[] newData = (E[])(new Object[size * 2 + 1]); 25 System.arraycopy(data, 0, newData, 0, size); 26 data = newData; 27 } 28 29 } 30 public void clear(){ 31 data = (E[])new Object[INITIAL_CAPACITY]; 32 size = 0; 33 } 34 public boolean contains(E e){ 35 for(int i = 0; i < size; i++){ 36 if(e.equals(data[i])) 37 return true; 38 } 39 return false; 40 } 41 public E get(int index){ 42 return data[index]; 43 } 44 public int indexOf(E e){ 45 for(int i = 0; i < size; i++){ 46 if(e.equals(data[i])) return i; 47 } 48 return -1; 49 } 50 public int lastIndexOf(E e){ 51 for(int i = size - 1; i >= 0; i--) 52 if(e.equals(data[i])) 53 return i; 54 return -1; 55 56 } 57 public E remove(int index){ 58 E e = data[index]; 59 for(int j = index; j < index - 1; j++) 60 data[j] = data[j+1]; 61 data[size-1] = null; 62 size--; 63 return e; 64 } 65 public E set(int index, E e){ 66 E old = data[index]; 67 data[index] = e; 68 return old; 69 } 70 public String toString(){ 71 StringBuilder result = new StringBuilder("["); 72 73 for(int i = 0; i < size; i++){ 74 result.append(data[i]); 75 if(i < size - 1) result.append(", "); 76 } 77 78 return result.toString() + "]"; 79 } 80 public void trimToSize(){ 81 if(size != data.length){ 82 E[] newData = (E[])(new Object[size]); 83 System.arraycopy(data, 0, newData, 0, size); 84 data = newData; 85 } 86 } 87 }
线性表测试TestList.java
1 public class TestList { 2 3 public static void main(String[] args) { 4 // TODO Auto-generated method stub 5 MyList<String> list = new MyArrayList<String>(); 6 7 list.add("American"); 8 System.out.println("(1)"+list); 9 10 list.add(0, "Canada"); 11 System.out.println("(2)"+list); 12 13 list.add("Russia"); 14 System.out.println("(3)"+list); 15 16 list.add("France"); 17 System.out.println("(4)"+list); 18 19 list.add(2, "Germany"); 20 System.out.println("(5)"+list); 21 22 list.add(5, "Normay"); 23 System.out.println("(6)"+list); 24 25 list.remove("Canada"); 26 System.out.println("(7)"+list); 27 28 list.remove(2); 29 System.out.println("(8)"+list); 30 31 list.remove(list.size() - 1); 32 System.out.println("(9)"+list); 33 34 35 } 36 37 }