线性表的性质
- MyArrrayList数组和MyLinkedList链表
- 相同的操作,不同的实现
如何设计
解决方案
- 接口和抽象类结合的方法
好的设计思路:同时设计抽象类和接口
http://www.cnblogs.com/dolphin0520/p/3811437.html具体的实现可以,结合接口和抽象类
- 固有行为放入abstract
- 附近延伸的行为放入interface
------------------------
<interface>
MyList<E>
-------------------------
+add(e:E):void
+add(index:int ,e:E):void
+clear():void
+contain(e:E):boolean
+get(index:int):E
+indexOf(e:E):int
+isEmpty():boolean
+lastIndexOf(e:E):int
+remove(e:E):boolean
+size():int
+remove(index:int):E
+set(index:int,e:E):E
-----------------------
MyAbstractList<E>
-----------------------
#size: int
-----------------------
#MyAbstractList()
#MyAbstratctList(objects:E[])
+add(e:E)
+isEmpty():boolean
+remove(e:E):boolean
代码
//MyList.java
public interface MyList<E>{
public void add(E e);
public void add(int index,E e);
public void clear();
public boolean contains(E e);
public E get(int index);
public int indexOf(E e);
public boolean isEmpty();
public int lastIndexOf(E e);
public boolean remove(E e);
public E remove(int index);
public Object set(int index,E e);
public int size();
}
public abstract class MyAbstractList<E> implements MyLsit<E>{
protected int size=0; //抽象类的数据域设计为保护模式
protected MyAbstractList(){
//构造函数
}
protected MyAbstractList(E[] object){
for(int i=0>i<object.length;i++)
add(object[i]);
}
public void add(E e){
add(size,e);
}
public boolean isEmpty(){
return size==0;
}
public int size(){
return size;
}
public boolean remove(E e){
if(indexOf(e)>=0){
remove(indexOf(e));
return true;
}
else
return false;
}
}
线性表的实现
public class MyArrayList<E> extends MyAbstractList<E> {
public static final int INITIAL_CAPACITY = 16;
private E[] data = (E[])new Object[INITIAL_CAPACITY];
/** Create a default list */
public MyArrayList() {
}
/** Create a list from an array of objects */
public MyArrayList(E[] objects) {
for (int i = 0; i < objects.length; i++)
add(objects[i]); // Warning: don抰 use super(objects)!
}
/** Add a new element at the specified index in this list */
public void add(int index, E e) {
ensureCapacity();
// Move the elements to the right after the specified index
for (int i = size - 1; i >= index; i--)
data[i + 1] = data[i];
// Insert new element to data[index]
data[index] = e;
// Increase size by 1
size++;
}
/** Create a new larger array, double the current size */
private void ensureCapacity() {
if (size >= data.length) {
E[] newData = (E[])(new Object[size * 2 + 1]);
System.arraycopy(data, 0, newData, 0, size);
data = newData;
}
}
/** Clear the list */
public void clear() {
data = (E[])new Object[INITIAL_CAPACITY];
size = 0;
}
/** Return true if this list contains the element */
public boolean contains(E e) {
for (int i = 0; i < size; i++)
if (e.equals(data[i])) return true;
return false;
}
/** Return the element from this list at the specified index */
public E get(int index) {
return data[index];
}
/** Return the index of the first matching element in this list.
* Return -1 if no match. */
public int indexOf(E e) {
for (int i = 0; i < size; i++)
if (e.equals(data[i])) return i;
return -1;
}
/** Return the index of the last matching element in this list
* Return -1 if no match. */
public int lastIndexOf(E e) {
for (int i = size - 1; i >= 0; i--)
if (e.equals(data[i])) return i;
return -1;
}
/** Remove the element at the specified position in this list
* Shift any subsequent elements to the left.
* Return the element that was removed from the list. */
public E remove(int index) {
E e = data[index];
// Shift data to the left
for (int j = index; j < size - 1; j++)
data[j] = data[j + 1];
data[size - 1] = null; // This element is now null
// Decrement size
size--;
return e;
}
/** Replace the element at the specified position in this list
* with the specified element. */
public E set(int index, E e) {
E old = data[index];
data[index] = e;
return old;
}
/** Override toString() to return elements in the list */
public String toString() {
StringBuilder result = new StringBuilder("[");
for (int i = 0; i < size; i++) {
result.append(data[i]);
if (i < size - 1) result.append(", ");
}
return result.toString() + "]";
}
/** Trims the capacity to current size */
public void trimToSize() {
if (size != data.length) { // If size == capacity, no need to trim
E[] newData = (E[])(new Object[size]);
System.arraycopy(data, 0, newData, 0, size);
data = newData;
}
}
}
main函数
public class TestList {
public static void main(String[] args) {
// Create a list
MyList<String> list = new MyArrayList<String>();
// Add elements to the list
list.add("America"); // Add it to the list
System.out.println("(1) " + list);
list.add(0, "Canada"); // Add it to the beginning of the list
System.out.println("(2) " + list);
list.add("Russia"); // Add it to the end of the list
System.out.println("(3) " + list);
list.add("France"); // Add it to the end of the list
System.out.println("(4) " + list);
list.add(2, "Germany"); // Add it to the list at index 2
System.out.println("(5) " + list);
list.add(5, "Norway"); // Add it to the list at index 5
System.out.println("(6) " + list);
// Remove elements from the list
list.remove("Canada"); // Same as list.remove(0) in this case
System.out.println("(7) " + list);
list.remove(2); // Remove the element at index 2
System.out.println("(8) " + list);
list.remove(list.size() - 1); // Remove the last element
System.out.println("(9) " + list);
}
}