一个简单的问题 用数组实现堆栈(stack)的方法 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用Stack类啊,自己看看Stack类 public class MyStack { Object[] obj = new Object[1]; public void push(Object o) { if (obj[0] != null) { //判断是否为空,数组不为空,数组大小加一,插入 Object[] temp = obj; //temp用来临时保存,复制 obj = new Object[obj.length + 1]; for (int j = 0; j < temp.length; j++) { obj[j] = temp[j]; } temp = null; obj[obj.length - 1] = o; //在末尾插入 } else { obj[0] = o; //数组为空,插入到首位 } } public Object pop() { if (obj[0]!=null){ //判断数组非空,取值 Object o = obj[obj.length - 1]; Object[] temp = obj; //temp用来临时保存,复制 obj = new Object[obj.length - 1]; //数组长度减一 for (int j = 0; j < temp.length-1; j++) { obj[j] = temp[j]; } temp = null; return o; } else { return null; //数组为空,不返回 } } public int search(Object o){ if(o!=null){ for(int i=0;i<obj.length;i++){ if(obj[i].equals(o))return i; } return -1; } else{ return -1; } } public static void main(String[] args) { MyStack myStack = new MyStack(); myStack.push(new String("abc")); myStack.push(new String("cba")); myStack.push(new Integer(111)); myStack.push(new Integer(222)); for (int i = 0; i < myStack.obj.length; i++) { System.out.println(myStack.obj[i]); } System.out.println("**********"); Object o1 = myStack.pop(); Object o2 = myStack.pop(); Object o3 = myStack.pop(); System.out.println(o1); System.out.println(o2); System.out.println(o3); System.out.println("**********"); for (int i = 0; i < myStack.obj.length; i++) { System.out.println(myStack.obj[i]); } }} 不过数组有大小限制 javascript的open()打开一个窗体如何实现? 请问如何实现类似ACDsee的略缩图浏览时的略缩图布局? 大家帮忙看一下这个代码 子类的getClone函数该如何写? 拜师 报个到,刚来,随便提个问 **jayde(胡不归) 和tuhongyu(牵着老鼠打劫) 进来** help!!! 各位老哥帮帮忙,这个问题。。。呵,呵,我不懂! 请问:如何获取系统时间???知道的赶快回答,给分的呦!! 急求有关java下文件并发操作的问题 关于java 游标 数据库 的问题??
Object[] obj = new Object[1];
public void push(Object o) {
if (obj[0] != null) { //判断是否为空,数组不为空,数组大小加一,插入
Object[] temp = obj; //temp用来临时保存,复制
obj = new Object[obj.length + 1];
for (int j = 0; j < temp.length; j++) {
obj[j] = temp[j];
}
temp = null;
obj[obj.length - 1] = o; //在末尾插入
}
else {
obj[0] = o; //数组为空,插入到首位
}
}
public Object pop() {
if (obj[0]!=null){ //判断数组非空,取值
Object o = obj[obj.length - 1];
Object[] temp = obj; //temp用来临时保存,复制
obj = new Object[obj.length - 1]; //数组长度减一
for (int j = 0; j < temp.length-1; j++) {
obj[j] = temp[j];
}
temp = null;
return o;
}
else {
return null; //数组为空,不返回
}
}
public int search(Object o){
if(o!=null){
for(int i=0;i<obj.length;i++){
if(obj[i].equals(o))return i;
}
return -1;
}
else{
return -1;
}
}
public static void main(String[] args) {
MyStack myStack = new MyStack();
myStack.push(new String("abc"));
myStack.push(new String("cba"));
myStack.push(new Integer(111));
myStack.push(new Integer(222));
for (int i = 0; i < myStack.obj.length; i++) {
System.out.println(myStack.obj[i]);
}
System.out.println("**********");
Object o1 = myStack.pop();
Object o2 = myStack.pop();
Object o3 = myStack.pop();
System.out.println(o1);
System.out.println(o2);
System.out.println(o3);
System.out.println("**********");
for (int i = 0; i < myStack.obj.length; i++) {
System.out.println(myStack.obj[i]);
}
}
} 不过数组有大小限制