小弟新学java,试着用动态数组实现stack(静态数组方法以实现)。要求不能用ArrayList和其他java library的class. 但是编译的时候却遇到错误,错误是在doubleArray方法和push()使用doubleArray()时。各位大哥可否帮忙看看那里有错,不胜感激:)interface DStack {
boolean isEmpty();
void push(double d);
double pop();
double top();
}public class ArrayStack implements DStack {
private double[] stack=new double[200]; //build stack array
private int m_size=0;//initialize Stack size
//Double the original array size and copy the original array to the new array
public void arrayDouble(){
double[] temp=stack;
int size=stack.length;
stack=new double[2*size];
for(int i=0;i<size;i++)
stack[i]=temp[i];
}
//Insert data to stack
public void push(double d){
if(m_size<=stack.length){
stack[m_size]=d;
m_size++;
}
else {
arrayDouble();
stack[m_size]=d;
m_size++;
}
}
//Remove the top element
public double pop(){
if(!this.isEmpty()){
double a=stack[m_size-1];
m_size--;
return a;
}
else{
throw new java.util.EmptyStackException();
}
}
//Return the top element
public double top(){
if(!this.isEmpty()){
return stack[m_size-1];
}
else{
throw new java.util.EmptyStackException();
}
}
//Check whether stack is empty or not
public boolean isEmpty(){
if(m_size<=0)
return true;
else
return false;
}
}
boolean isEmpty();
void push(double d);
double pop();
double top();
}public class ArrayStack implements DStack {
private double[] stack=new double[200]; //build stack array
private int m_size=0;//initialize Stack size
//Double the original array size and copy the original array to the new array
public void arrayDouble(){
double[] temp=stack;
int size=stack.length;
stack=new double[2*size];
for(int i=0;i<size;i++)
stack[i]=temp[i];
}
//Insert data to stack
public void push(double d){
if(m_size<=stack.length){
stack[m_size]=d;
m_size++;
}
else {
arrayDouble();
stack[m_size]=d;
m_size++;
}
}
//Remove the top element
public double pop(){
if(!this.isEmpty()){
double a=stack[m_size-1];
m_size--;
return a;
}
else{
throw new java.util.EmptyStackException();
}
}
//Return the top element
public double top(){
if(!this.isEmpty()){
return stack[m_size-1];
}
else{
throw new java.util.EmptyStackException();
}
}
//Check whether stack is empty or not
public boolean isEmpty(){
if(m_size<=0)
return true;
else
return false;
}
}
// Insert data to stack
public void push(double d) {
//这里用小于,不要小于等于,否则会出现数组越界
if (m_size < stack.length) {
stack[m_size] = d;
m_size++;
}
else {
arrayDouble();
stack[m_size] = d;
m_size++; }
}
private double[] stack = new double[200]; // build stack array
private int m_size = 0;// initialize Stack size // Double the original array size and copy the original array to the new
// array
public void arrayDouble() {
double[] temp = stack;
int size = stack.length;
stack = new double[2 * size];
for (int i = 0; i < size; i++)
stack[i] = temp[i];
} // Insert data to stack
public void push(double d) {
if (m_size < stack.length) {
stack[m_size] = d;
m_size++;
} else {
arrayDouble();
stack[m_size] = d;
m_size++;
}
} // Remove the top element
public double pop() {
if (!this.isEmpty()) {
double a = stack[m_size - 1];
m_size--;
return a;
} else {
throw new java.util.EmptyStackException();
}
} // Return the top element
public double top() {
if (!this.isEmpty()) {
return stack[m_size - 1];
} else {
throw new java.util.EmptyStackException();
}
} // Check whether stack is empty or not
public boolean isEmpty() {
if (m_size <= 0)
return true;
else
return false;
}
public static void main(String[] args) {
ArrayStack arrayStack = new ArrayStack();
for (int i = 0; i < 201; i ++) {
arrayStack.push(1.1);
}
System.out.println(arrayStack.m_size);
}
}