Bevor ich beginne, entschuldige ich mich, wenn dies schwer zu lesen ist, ist es aus einem Buch, und ich darf nicht viel ändern. Ich muss eine Methode namens Peek() implementieren, die den Anfang des Stapels überprüft und dann angezeigt wird.Spähen Anfang des Stapels verursacht Out-of-Bounds-Ausnahme
Ich habe reset() und Größe() arbeiten, aber warum Peek ist() funktioniert nicht?
interface ISimpleStack {
// Push a character onto the stack.
void push(char ch);
// Pop a character from the stack.
char pop();
// Return true if the stack is empty.
boolean isEmpty();
// Return true if the stack is full.
boolean isFull();
void reset();
char peek();
int size();
}
// A fixed-length stack for characters.
class FixedLengthStack implements ISimpleStack {
private char[] data; // this array holds the stack
private int tos; // index of top of stack
// Construct an empty stack given its size.
FixedLengthStack(int size) {
data = new char[size]; // create the array to hold the stack
tos = 0;
}
// Construct a stack from a stack.
FixedLengthStack(FixedLengthStack otherStack) {
// size of new stack equals that of otherStack
data = new char[otherStack.data.length];
// set tos to the same position
tos = otherStack.tos;
// copy the contents
for(int i = 0; i < tos; i++)
data[i] = otherStack.data[i];
}
// Construct a stack with initial values.
FixedLengthStack(char[] chrs) {
// create the array to hold the initial values
data = new char[chrs.length];
tos = 0;
// initialize the stack by pushing the contents
// of chrs onto it
for(char ch : chrs)
push(ch);
}
// Push a character onto the stack.
public void push(char ch) {
if(isFull()) {
System.out.println(" -- Stack is full.");
return;
}
data[tos] = ch;
tos++;
}
// Pop a character from the stack.
public char pop() {
if(isEmpty()) {
System.out.println(" -- Stack is empty.");
return (char) 0; // a placeholder value
}
tos--;
return data[tos];
}
// Return true if the stack is empty.
public boolean isEmpty() {
return tos==0;
}
// Return true if the stack is full.
public boolean isFull() {
return tos==data.length;
}
public void reset() {
tos = 0;
}
public char peek() {
if(isEmpty()) {
System.out.println(" -- Stack is empty.");
return (char) 0; // a placeholder value
}
return data[tos];
}
public int size() {
int size = 0;
for(int i = 0; i <= tos; i++){
size = i;
}
System.out.println("Size of stack is: " + size);
return size;
}
}
// A growable stack for characters.
class DynamicStack implements ISimpleStack {
private char[] data; // this array holds the stack
private int tos; // index of top of stack
// Construct an empty stack given its size.
DynamicStack(int size) {
data = new char[size]; // create the array to hold the stack
tos = 0;
}
// Construct a stack from a stack.
DynamicStack(DynamicStack otherStack) {
// size of new stack equals that of otherStack
data = new char[otherStack.data.length];
// set tos to the same position
tos = otherStack.tos;
// copy the contents
for(int i = 0; i < tos; i++)
data[i] = otherStack.data[i];
}
// Construct a stack with initial values.
DynamicStack(char[] chrs) {
// create the array to hold the initial values
data = new char[chrs.length];
tos = 0;
// initialize the stack by pushing the contents
// of chrs onto it
for(char ch : chrs)
push(ch);
}
// Push a character onto the stack.
public void push(char ch) {
// if there is no more room in the array,
// expand the size of the stack
if(tos == data.length) {
// double the size of the existing array
char[] t = new char[data.length * 2];
// copy the contents of the stack into the larger array
for(int i = 0; i < tos; i++)
t[i] = data[i];
// set data to refer to the new array
data = t;
}
data[tos] = ch;
tos++;
}
// Pop a character from the stack.
public char pop() {
if(isEmpty()) {
System.out.println(" -- Stack is empty.");
return (char) 0; // a placeholder value
}
tos--;
return data[tos];
}
// Return true if the stack is empty.
public boolean isEmpty() {
return tos==0;
}
// Return true if the stack is full. For DynamicStack,
// this method always returns false.
public boolean isFull() {
return false;
}
public void reset() {
tos = 0;
}
public char peek() {
if(isEmpty()) {
System.out.println(" -- Stack is empty.");
return (char) 0; // a placeholder value
}
return data[tos];
}
public int size() {
int size = 0;
for(int i = 0; i <= tos; i++){
size = i;
}
System.out.println("Size of stack is: " + size);
return size;
}
}
// Demonstrate ISimpleStack.
class ISimpleStackDemo {
public static void main(String[] args) {
int i;
char ch;
// create an ISimpleStack interface variable
ISimpleStack iStack;
// Now, construct a FixedLengthStack and a DynamicStack
FixedLengthStack fixedStack = new FixedLengthStack(10);
DynamicStack dynStack = new DynamicStack(5);
// first, use fixedStack through iStack
iStack = fixedStack;
// push characters onto fixedStack
for(i = 0; !iStack.isFull(); i++)
iStack.push((char) ('A'+i));
iStack.size();
System.out.print("Top of fixedStack: ");
while(!iStack.isEmpty()) {
ch = iStack.peek();
System.out.print(ch);
}
// pop characters off fixedStack
System.out.print("Contents of fixedStack: ");
while(!iStack.isEmpty()) {
ch = iStack.pop();
System.out.print(ch);
}
System.out.print("\nContents of fixedStack after reset: ");
iStack.reset();
ch = iStack.pop();
System.out.print(ch);
System.out.println();
// next, use dynStack through iStack
iStack = dynStack;
// push A through Z onto dynStack
// this will result in three increases in its size
for(i = 0; i < 26; i++)
iStack.push((char) ('A'+i));
iStack.size();
// pop characters off dynStack
System.out.print("Contents of dynStack: ");
while(!iStack.isEmpty()) {
ch = iStack.pop();
System.out.print(ch);
}
System.out.print("\nContents of dynStack after reset: ");
iStack.reset();
ch = iStack.pop();
System.out.print(ch);
}
}
Wenn das Array 0 basiert, und Sie nach einem Push inkrementieren, dann wird es über das Ende des Arrays hinaus sein, nicht? Sie müssen auf Daten [data.length - 1] schauen. – KevinO
Ausgabe: http://pastebin.com/Mi1vBfSY Dies stoppte den Fehler, aber es zeigt nicht die zweite. –
Haben Sie die gleiche Änderung vorgenommen? Die Variable tos ist die _size_ des Stapels, aber um das Zeichen zu finden, das sich an der letzten Position befindet, müssen Sie 'data [tos - 1]' betrachten. – KevinO