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-武恺莉1407094208 #16

Open
kailiwu opened this issue Dec 22, 2015 · 0 comments
Open

Java1-武恺莉1407094208 #16

kailiwu opened this issue Dec 22, 2015 · 0 comments

Comments

@kailiwu
Copy link

kailiwu commented Dec 22, 2015

1.删除后三只羊
        //第一种方法
        int count=sheeplist.size();
        for(int i=count-1;i>=count-3;i--){
            sheeplist.remove(i);
        }
        //第二种方法
        for(int i=sheeplist.size()-3;i<sheeplist.size();){
            sheeplist.remove(i);
        }
        //第三种方法
        int count1=0;
        while(count1<3){
            sheeplist.remove(sheeplist.size()-1);
            count++;
        }
        //第四种方法
        int count2=sheeplist.size();
        for(int i=0;i<sheeplist.size();i++){
            while(i==count2-3){
                sheeplist.remove(i);
                i=i-1;
            }
        }
2.自动写文件
for(int i=0;i<10;i++){
        File filei=new File(files, "helloworld"+i+".txt");
        try {
            OutputStream os=new FileOutputStream(filei);
            String s="我真";
            s=s+i;
            os.write(s.getBytes());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
3.线程实现方法继承Thread与接口Runnnale的区别
(1)继承自Thread
        ThreadExtend t1=new ThreadExtend("窗口一");
        ThreadExtend t2=new ThreadExtend("窗口二");
        ThreadExtend t3=new ThreadExtend("窗口三");
        t1.start();
        t2.start();
        t3.start();
class ThreadExtend extends Thread{
    private int tickets=5;
    public ThreadExtend(String name) {
        super(name);
    }
    @Override
    public void run() {
        while(this.tickets>0){
            this.tickets--;
        System.out.println("票在"+this.getName()+"被卖出");
        }
    }
}
(2)Runnable接口
        ThreadExtend t=new ThreadExtend();
        Thread c1=new Thread(t,"Runnable1");
        Thread c2=new Thread(t,"Runnable2");
        Thread c3=new Thread(t,"Runnable3");
        c1.start();
        c2.start();
        c3.start();
class ThreadExtend implements Runnable{
    private int ticket=5;
    @Override
    public void run() {
        while(this.ticket>0){
            this.ticket--;
        System.out.println("票在"+Thread.currentThread().getName()+"被卖出");
        }
        }

}
继承自Thread只能是单继承,每个线程之间是独立的;Runnnale接口可以实现线程间的资源共享。
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