Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java1-赵菲菲1414010509 #23

Open
ZhFeiFei opened this issue Dec 24, 2015 · 0 comments
Open

Java1-赵菲菲1414010509 #23

ZhFeiFei opened this issue Dec 24, 2015 · 0 comments

Comments

@ZhFeiFei
Copy link

删羊操作

    //利用迭代器遍历羊群并删除。
        int i;
        public void Iterator{
            Sheep[] sheep = new Sheep[] {new Sheep("喜羊羊"), new Sheep("美羊羊"), new Sheep("慢羊羊"), new Sheep("沸羊羊"),
            new Sheep("懒羊羊")};
            Iterator sum=sheep.Iterator();
            while(sum.hasNext()){
                Sheep sheep=(Sheep)sum.next();  
                sum.remove();
            }
        }

        //寻常的for循环从前向后删
        for(i=1;i<sheepList.size()+1;i++){
            sheepList.remove(i);
        }

        //从后向前删
        for(i=sheepList.size()-1;i>0;i--){
            sheepList.remove(i);
        }

自动写文件

       //  先生成文件,然后再执行出错跟踪和输入操作。
        int a;
        Scanner reader=new Scanner(System.in);
        a=reader.nextInt();
        while(a){
            File file=new File("D://helloworld"+i+".txt");
            OutputStream outputstream=null;
            try{
                outputstream=new FileOutputStream(file);
                String s="我是"+i;    
                outputstream.write(s)
            }
            catch(IOException e){
                e.printStackTrace();
            }
            finally{
                try{
                    file.close();
                }
                catch(IOException e){
                    e.printStackTrace()
                }
            }

        }

实现Thread和Runnable接口的比较

          //Thread和Runnable的区别:
          //         Thread是类,在Thread中只能利用start方法实现run方法,且在执行时需要等待。
         //          Runnable是接口,被继承时需要重写run方法,在现实该方法时也要利用start方法,且在
         //       执行时不等待 ,直接抢先执行。
        public HtThread extends Thread{
        private int i;
        public void run(){
            for(i=0;i<=100;i++){
                System.out.println("HtThread-->"+i);
            }
        }
        public HtRunnable implementsRunnable{
        private int i;
        public void run(){
            for(i=0;i<=100;i++){
                System.out.println("HtRunnable-->"+i);
            }
        }

        public static void main(String args[]){
            HtThread thread=new HtThread();
            HtRunnable runnable=new HtRunnable();
            thread.start();
            for(int i=0;i<=100;i++){
                System.out.println("test-->"+i);
            }
            //runnable.start();
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant