| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 713 人关注过本帖
标题:stack的实现
只看楼主 加入收藏
chianti
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2005-5-1
收藏
 问题点数:0 回复次数:5 
stack的实现

请指正有不足的地方 import java.util.*; import java.io.*; class stack1 { public static void main(String args[]) { operate he=new operate(); int k;int y=1; DataInputStream ha1=new DataInputStream(System.in); while(y!=0) {try {he.menu(); k=Integer.parseInt(ha1.readLine()); switch(k) {case 1 :{he.enter();break;} case 2 : {he.remove();break;} case 3 : {he.display();break;} case 4 : {he.clear();break;} case 5 : {y--;break;} default :break; } } catch (Exception e) {System.out.println("---input error,please input

right order");} } } } class operate { ArrayList al=new ArrayList(); void menu() {System.out.println("-----menu of the program"); System.out.println("1).enter the value in the

stack"); System.out.println("2).remove the last value from

the stack"); System.out.println("3).display the value"); System.out.println("4).clear all the value"); System.out.println("5).exit"); System.out.print("I choose:"); } void enter() {int y=1,k; DataInputStream ha2=new DataInputStream(System.in); while(y!=0) { try { System.out.print("enter you number :"); int i; i=Integer.parseInt(ha2.readLine()); al.add(new Integer(i)); y--; System.out.println("--type 1 to enter more"); k=Integer.parseInt(ha2.readLine()); if(k==1) y++; } catch (Exception e) {} } } void remove() { int i,y=1,k; i=al.size(); if(i!=0) { DataInputStream ha3=new DataInputStream(System.in); while(y!=0) {try {al.remove(i-1); i=i-1; System.out.println("--type 1 to remove more"); k=Integer.parseInt(ha3.readLine()); if(k==1) y++; } catch (Exception e) {} } } else System.out.println("--null"); } void display() { int i; i=al.size(); if(i!=0) System.out.println(al); else System.out.println("--null"); } void clear() { al.clear();} }

搜索更多相关主题的帖子: stack 
2005-05-14 10:41
tempnetbar
Rank: 2
等 级:新手上路
威 望:4
帖 子:582
专家分:4
注 册:2004-5-5
收藏
得分:0 
这个……似乎Collection里有Stack,不用自己写吧?

相信勤能补拙! 喜欢用好用的就永远学不到有用的。
2005-05-14 18:05
chianti
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2005-5-1
收藏
得分:0 
void remove()
        { int i,y=1,k;
         i=al.size();
         if(i!=0)   {
            DataInputStream ha3=new DataInputStream(System.in);
             while(y!=0)
              {try
                {al.remove(i-1);
                 i=i-1;
                 y=y-1;//写错的地方,前面的少了一个y--;sorry
                 System.out.println("--type 1 to remove more");
                 k=Integer.parseInt(ha3.readLine());
                 if(k==1) y++;
                }
               catch (Exception e)
                {}
              }         
                     }
          else System.out.println("--null");
        }
2005-05-15 11:08
chianti
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2005-5-1
收藏
得分:0 
斑竹:
我是练习呀,我也是这个学期才开的课
2005-05-15 11:10
tempnetbar
Rank: 2
等 级:新手上路
威 望:4
帖 子:582
专家分:4
注 册:2004-5-5
收藏
得分:0 
这样啊,那你的程序最大的不足就是非线程安全,这个一般初学很难想到的,你去看看JSDK源代码里Stack的实现就知道了,要有同步代码块,在push和pop时,还有就是代码不够简洁,你最好试着自己该动一下,这样会比较有收获。

相信勤能补拙! 喜欢用好用的就永远学不到有用的。
2005-05-16 09:13
Doctor
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2005-5-17
收藏
得分:0 
你的代块enter()和remove()方法中要用到线程的协作,防止线程间的互斥,不然打出来的东西就全乱了!建议1:用synchronized分别将两方法中的输入输出语句包起来!建议2:用线程协作做,这里就不多讲了,自己在网上找一下有关资料吧!

我本将心向明月,奈何明月照沟渠。 知我者谓我心忧,不知我者谓我何求?
2005-06-06 17:02
快速回复:stack的实现
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011510 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved