实现一个栈操作。

 1 import java.util.*;
 2 
 3 public class Stacks {
 4     static String[] months={"January","February","March","April","May","June",
 5         "July","August","September","October","November"};//定义字符串数组,使用大括号括了几个带双引号的字符串。
 6     
 7     public static void main(String[] args){
 8         Stack stk=new Stack();
 9         for(int i=0;i<months.length;i++){
10             stk.push(months[i]+"");
11             System.out.println("stk="+stk);
12             //把栈当作一个Vector。
13             stk.addElement("The last line");
14             System.out.println("element S="+stk.elementAt(5));
15             System.out.println("popping elements:");
16             while(!stk.empty())
17                 System.out.println(stk.pop());
18         
19     }
20     
21 
22 }
23 }

前面多加了一对{},导致出错了。正确的代码,去掉那俩大括号,一对。。。

 1 import java.util.*;
 2 
 3 public class Stacks {
 4     static String[] months={"January","February","March","April","May","June",
 5         "July","August","September","October","November"};//定义字符串数组,使用大括号括了几个带双引号的字符串。
 6     
 7     public static void main(String[] args){
 8         Stack stk=new Stack();
 9         for(int i=0;i<months.length;i++)
10             stk.push(months[i]+"");
11             System.out.println("stk="+stk);
12             //把栈当作一个Vector。
13             stk.addElement("The last line");
14             System.out.println("element S="+stk.elementAt(5));
15             System.out.println("popping elements:");
16             while(!stk.empty())
17                 System.out.println(stk.pop());
18         
19     }
20     
21 
22 }
原文地址:https://www.cnblogs.com/meihao1989/p/3305041.html