JAVA数组测试题(模拟栈)
- 使用一维数组,模拟栈数据结构
1、这个栈可以存储java中的任何引用类型数据
2、在栈中提供push方法模拟压栈(栈满了要有提示信息)
3、在栈中提供pop方法模拟弹栈(栈空了也有提示信息)
4、编写测试程序,new栈对象,调用push 和 pop方法来模拟压栈弹栈动作
public class MyStack {
public static void main(String[] args) {
Animal a1 = new Cat();
Animal a2 = new Bird();
Animal[] animals = {a1, a2};
Animal[] animals1 = {a1};
Stack stack = new Stack();
stack.push(animals);
for (int i = 0; i < stack.elements.length; i++) {
System.out.println(stack.elements[i]);
}
System.out.println("==============================================");
stack.push(animals);
stack.push(animals);
stack.push(animals);
stack.push(animals);
stack.push(animals1);
stack.push(animals1);
for (int i = 0; i < stack.elements.length; i++) {
System.out.println(stack.elements[i]);
}
System.out.println("==============================================");
stack.pop(3);
for (int i = 0; i < stack.elements.length; i++) {
System.out.println(stack.elements[i]);
}
System.out.println("==============================================");
stack.pop();
for (int i = 0; i < stack.elements.length; i++) {
System.out.println(stack.elements[i]);
}
System.out.println("==============================================");
stack.pop(5);
for (int i = 0; i < stack.elements.length; i++) {
System.out.println(stack.elements[i]);
}
System.out.println("==============================================");
stack.pop();
for (int i = 0; i < stack.elements.length; i++) {
System.out.println(stack.elements[i]);
}
}
}
class Stack {
Object[] elements;
int cnt;
public Stack(){
elements = new Object[9];
cnt = 0;
}
public void push (Object[] obj){
if (cnt == elements.length){
System.out.println("栈满");
System.out.println("==============================================");
return;
}else if ((cnt + obj.length) > elements.length){
System.out.println("栈溢出,压栈失败");
System.out.println("==============================================");
return;
}
System.arraycopy(obj, 0, elements, cnt, obj.length);
cnt = cnt + obj.length;
}
public void pop (){
if (cnt == 0){
System.out.println("栈空");
System.out.println("==============================================");
return;
}
elements[cnt - 1] = null;
cnt--;
}
public void pop (int i){
if (cnt == 0){
System.out.println("栈空");
System.out.println("==============================================");
return;
}
for (int j = cnt; j > cnt - i; j--){
elements[j - 1] = null;
}
cnt = cnt - i;
}
}
array.Cat@1d81eb93
array.Bird@7291c18f
null
null
null
null
null
null
null
==============================================
栈溢出,压栈失败
==============================================
栈满
==============================================
array.Cat@1d81eb93
array.Bird@7291c18f
array.Cat@1d81eb93
array.Bird@7291c18f
array.Cat@1d81eb93
array.Bird@7291c18f
array.Cat@1d81eb93
array.Bird@7291c18f
array.Cat@1d81eb93
==============================================
array.Cat@1d81eb93
array.Bird@7291c18f
array.Cat@1d81eb93
array.Bird@7291c18f
array.Cat@1d81eb93
array.Bird@7291c18f
null
null
null
==============================================
array.Cat@1d81eb93
array.Bird@7291c18f
array.Cat@1d81eb93
array.Bird@7291c18f
array.Cat@1d81eb93
null
null
null
null
==============================================
null
null
null
null
null
null
null
null
null
==============================================
栈空
==============================================
null
null
null
null
null
null
null
null
null
Process finished with exit code 0