patternjavaMinor
Stack with Array
Viewed 0 times
witharraystack
Problem
Just coded up a simple Stack using an Array in Java for practice. Other than the lack of JavaDoc (still need to learn that part), did I cover all the bases?
```
import java.util.Iterator;
public class ArrayStack implements Iterable {
private T[] stack;
private int top = 0; //Position of the top of the stack
//Constructor
public ArrayStack(int size) {
//Generic array creation isn't allowed, using cast
stack = (T[]) new Object[size];
} //End Constructor
//Add item to top of stack
public void push(T item) {
//If you've reached top of stack, double it's size
if (top == stack.length) {
resize(stack.length *2);
}
stack[top++] = item;
StdOut.println(stack[top-1] + " Added to stack"); //For debugging
} //End push
//Removes item from top of stack
public T pop() {
T toReturn = stack[--top];
stack[top] = null; //Garbage cleanup
//Reduce size of array if it is less than 1/3rd full
if (top 10) {
resize(stack.length/2);
}
return toReturn;
} //End pop
//Simple true or false if the stack is empty or not
public boolean isEmpty() {
return (top == 0);
} //End isEmpty
//Returns the iterator
public Iterator iterator() {
return new StackIterator();
} //End iterator
public class StackIterator implements Iterator {
private int i = top; //Increment starting at top of stack
@Override
//True until i is the bottom of the stack
public boolean hasNext() {
return (i > 0);
} //End hasNext
@Override
//Returns the items in the stack starting from top
public T next() {
return stack[--i];
} //End next
@Override
//Not implemented
public void remove() { }
} //End StackIterator
//Creates a new array of the passed in size a
```
import java.util.Iterator;
public class ArrayStack implements Iterable {
private T[] stack;
private int top = 0; //Position of the top of the stack
//Constructor
public ArrayStack(int size) {
//Generic array creation isn't allowed, using cast
stack = (T[]) new Object[size];
} //End Constructor
//Add item to top of stack
public void push(T item) {
//If you've reached top of stack, double it's size
if (top == stack.length) {
resize(stack.length *2);
}
stack[top++] = item;
StdOut.println(stack[top-1] + " Added to stack"); //For debugging
} //End push
//Removes item from top of stack
public T pop() {
T toReturn = stack[--top];
stack[top] = null; //Garbage cleanup
//Reduce size of array if it is less than 1/3rd full
if (top 10) {
resize(stack.length/2);
}
return toReturn;
} //End pop
//Simple true or false if the stack is empty or not
public boolean isEmpty() {
return (top == 0);
} //End isEmpty
//Returns the iterator
public Iterator iterator() {
return new StackIterator();
} //End iterator
public class StackIterator implements Iterator {
private int i = top; //Increment starting at top of stack
@Override
//True until i is the bottom of the stack
public boolean hasNext() {
return (i > 0);
} //End hasNext
@Override
//Returns the items in the stack starting from top
public T next() {
return stack[--i];
} //End next
@Override
//Not implemented
public void remove() { }
} //End StackIterator
//Creates a new array of the passed in size a
Solution
For starters, comment pairs such as
Your debugging statements can be better served using a standard logging library that lets you log statements categorized by severity, e.g.
Instead of creating your inner class
You can also see that
Last but not least, I'm not sure how much
//Constructor ... //End Constructor are redundant and actually makes the code a little harder to read. Your debugging statements can be better served using a standard logging library that lets you log statements categorized by severity, e.g.
org.slf4j.Logger.debug().Instead of creating your inner class
StackIterator, you can consider in-lining the implementation in the following way:public Iterator iterator() {
return new Iterator(){
private int i = top; //Increment starting at top of stack
@Override
public boolean hasNext() {
return i > 0;
}
@Override
public T next() {
// suggested by @Landei and @AluanHadda
if (!hasNext()) {
throw new NoSuchElementException();
}
return stack[--i];
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}You can also see that
remove() throws an UnsupportedOperationException(), to make the (non-)usage more apparent.Last but not least, I'm not sure how much
reinventing-the-wheel do you intend/expect to do here, but you can also look into using Arrays.copyOf() to handle the array resizing.Code Snippets
public Iterator<T> iterator() {
return new Iterator<T>(){
private int i = top; //Increment starting at top of stack
@Override
public boolean hasNext() {
return i > 0;
}
@Override
public T next() {
// suggested by @Landei and @AluanHadda
if (!hasNext()) {
throw new NoSuchElementException();
}
return stack[--i];
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}Context
StackExchange Code Review Q#80315, answer score: 3
Revisions (0)
No revisions yet.