package coursera.week2.stackqueue;
/**
* This class uses an array to initialize a stack.
* @author hp-pc0
*
*/
public class FixedCapacityOfStack {
/**
* create a string array.
*/
private String[] s = null;
/**
* init the size of the array to 0.
*/
private int n = 0;
/**
* @param capacity constructor with paragram capacity.
*/
public FixedCapacityOfStack(final int capacity) {
s = new String[capacity];
}
/**
* @return
* check if the stack is empty.
*/
public final boolean isEmpty() {
return n == 0;
}
/**
* @param item
* the item to be push to the stack
*/
public final void push(final String item) {
s[n++] = item;
}
/**
* @return
* return the top element of the stack.
*/
public final String pop() {
String item = s[--n];
//to avoid memory litoring
s[n] = null;
return item;
}
}
stack: resize-array implementation
push(): 如果array满了之后,则双倍申请空间
pop(),如果array是1/4满的时候,将array大小减半。
对于链表表示的stack和array表示的stack,链表能保证整个速度均匀,array可能会在某个时间访问速度突然变慢,因为resize的原因。如果想要每个操作都要很快完成,则使用链表;如果想要总的操作时间比较快,则使用array,因为它的单个操作非常快,除了resize的时间。
泛型编程
package coursera.week2.stackqueue;
public class FixedCapacityStack<Item> {
/**
* create a string array.
*/
private Item[] s = null;
/**
* init the size of the array to 0.
*/
private int n = 0;
/**
* @param capacity constructor with paragram capacity.
*/
public FixedCapacityStack(final int capacity) {
//s = new Item[capacity];
s = (Item[])new Object[capacity];
}
/**
* @return
* check if the stack is empty.
*/
public final boolean isEmpty() {
return n == 0;
}
/**
* @param item
* the item to be push to the stack
*/
public final void push(final Item item) {
s[n++] = item;
}
/**
* @return
* return the top element of the stack.
*/
public final Item pop() {
Item item = s[--n];
//to avoid memory litoring
s[n] = null;
return item;
}
}
下边的程序时泛型编程应用的例子。
//MyStack myStack = new MyStack<Integer>(); this is not safe
MyStack<Integer> myStack = new MyStack<>(); //this is safe
//MyStack<Integer> myStack = new MyStack<Integer>(); //this is safe
myStack.push(12);
int a = myStack.pop();
迭代器
Q:什么是Iterable?
A:返回一个Iterator;
public interface Iterable<T> {
/**
* Returns an iterator over a set of elements of type T.
*
* @return an Iterator.
*/
Iterator<T> iterator();
}
Q:什么是Iterator?
A:含有hasNext()和next()
public interface Iterator<E> {
/**
* Returns {@code true} if the iteration has more elements.
* (In other words, returns {@code true} if {@link #next} would
* return an element rather than throwing an exception.)
*
* @return {@code true} if the iteration has more elements
*/
boolean hasNext();
/**
* Returns the next element in the iteration.
*
* @return the next element in the iteration
* @throws NoSuchElementException if the iteration has no more elements
*/
E next();
/**
* Removes from the underlying collection the last element returned
* by this iterator (optional operation). This method can be called
* only once per call to {@link #next}. The behavior of an iterator
* is unspecified if the underlying collection is modified while the
* iteration is in progress in any way other than by calling this
* method.
*
* @throws UnsupportedOperationException if the {@code remove}
* operation is not supported by this iterator
*
* @throws IllegalStateException if the {@code next} method has not
* yet been called, or the {@code remove} method has already
* been called after the last call to the {@code next}
* method
*/
void remove();
}
如果我们想让某个类能使用iterator,只需要让该类实现iterable接口即可。
package coursera.week2.stackqueue;
import java.util.Iterator;
import org.omg.CORBA.Current;
public class MyStack<Item> implements Iterable<Item>{
private Node first = null;
private class Node {
Item item;
Node next;
}
public boolean isEmpty() {
return first == null;
}
public void push(Item item) {
Node oldFirst = first;
first = new Node();
first.item = item;
first.next = oldFirst;
}
public Item pop(){
Item item = first.item;
first = first.next;
return item;
}
@Override
public Iterator<Item> iterator() {
return new ListIterator();
}
public class ListIterator implements Iterator<Item>{
private Node current = first;
@Override
public boolean hasNext() {
return current != null;
}
@Override
public Item next() {
Item item = current.item;
current = current.next;
return item;
}
@Override
public void remove() {
// TODO Auto-generated method stub
//抛出unsupportException()
}
}
}