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-权睿杰1407034148 #27

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

java1-权睿杰1407034148 #27

quanruijie opened this issue Dec 24, 2015 · 0 comments

Comments

@quanruijie
Copy link

作业一:删羊
package qrj.com;
import java.util.ArrayList;

public class Java_homework1 {
    public static void main(String[] args) {
        ArrayList sheepList=new ArrayList();
        sheepList.add(new Sheep("喜洋洋"));
        sheepList.add(new Sheep("懒洋洋"));
        sheepList.add(new Sheep("美洋洋"));
        sheepList.add(new Sheep("沸洋洋"));
        sheepList.add(new Sheep("慢洋洋"));
//删羊方法一:
        int len=sheepList.size();   //从后往前删
        for(int i=sheepList.size()-1;i>1;i--){
            sheepList.remove(i);
        }
//方法二:
        for(int i=0;i<3;i++){   //从末尾删三次
            sheepList.remove(sheepList.size()-1);
        }
//方法三:
        for(int i=2;i<sheepList.size();){
            sheepList.remove(i);
        }
//方法四:
        for(int i=0;i<sheepList.size();i++){    //删除羊(方法特殊)
                sheepList.remove(sheepList.size()-1);
        }   
//输出方法一:
        for(int i=0;i<sheepList.size();i++){
            System.out.println(((Sheep)(sheepList.get(i))).name);   //这里注意格式转化
        }
//输出方法二:
        for(Object s:sheepList){        //for(元素类型 元素名,List)
            System.out.println(((Sheep)s).name);
        }
    }
}


作业二:文件操作
package qrj.com;
import java.io.*;

public class Java_homework2 {

    public static void main(String[] args) {
        File file=new File("G://java");
        File myfile=null;
        OutputStream os=null;
        try {
            if(!file.exists()){
                file.mkdir();
            }
            for(int i=0;i<10;i++){
                myfile = New File(file.getAbsoluteFile(),"java "+i+" .txt");
                if(!myfile.exists())
                    myfile.createNewFile();
                try {
                    os=new FileOutputStream(myfile);
                    String s="我真"+i;
                    try {
                        os.write(s.getBytes());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }catch(IOException e) {
            e.printStackTrace();
        }finally{
            try{
                os.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }

}


作业三:两种创建并启动线程方法的比较
package qrj.com;
public class Java_homework3 {

public static void main(String[] args) {
    //创建并启动继承线程
    myThread t1=new myThread("the fist");
    myThread t2=new myThread("the second");
    myThread t3=new myThread("the thrid");
    t1.start();
    t2.start();
    t3.start();
    //创建并启动接口线程
    Thread s1=new Thread(new Thread2("the 1.."));
    Thread s2=new Thread(new Thread2("the 2.."));
    Thread s3=new Thread(new Thread2("the 3.."));
    s1.start();
    s2.start();
    s3.start();
}
}

class myThread extends Thread{ //Thread的继承类
    public myThread(String name){
        super(name);
    }
    public myThread(){
        super();
    }
    public void run(){
        for(int i=0;i<3;i++){
            System.out.println("继承类..."+this.getName()+i);
        }
    }
}

class Thread2 implements Runnable{ //实现接口的类
    public String name;
    public Thread2(){}
    public Thread2(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
    public void run(){
        for(int i=0;i<3;i++){
            System.out.println("实现接口的类..."+this.getName()+i);
        }
    }
    public void setName(String name){
        this.name=name;
    }
}
实现Runnable接口和继承Thread类创建并启动线程的区别:
前者可共享同一资源,后者不可;前者用途比较广,不可用this直接显示当前线程;后者操作简单,可以用this显示当前线程。最总要的一点,前者可以共用同一资源,后者不可。
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