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-杨娜1414011005 #17

Open
123yangna opened this issue Dec 22, 2015 · 0 comments
Open

Java1-杨娜1414011005 #17

123yangna opened this issue Dec 22, 2015 · 0 comments

Comments

@123yangna
Copy link

1. 删羊

方法1for (int i = 1; i <=sheepList.size(); i++) {
        sheepList.remove(2);
        }
方法2for(int i=sheepList.size()-1;i>1;i--){
        sheepList.remove(i);
        }
方法3System.out.println(sheepList.subList(0, 2));
    for (int i = 0; i < sheepList.size(); i++) {
            System.out.println(sheepList.get(i));
        }
方法4Iterator it=sheepList.iterator();
       while(it.hasNext()){
       Sheep sheepList1=(Sheep)it.next();
          if(sheepList1.getName()=="慢羊羊"||sheepList1.getName()=="沸羊羊"
               ||sheepList1.getName()=="懒羊羊"){
            it.remove();
            }
         }
方法5:
    int count=0;
    int len=sheepList.size();
    Iterator it=sheepList.iterator();
    while(it.hasNext()){
    if(count >= len - 3){
        Object ed=it.next();
        it.remove();
            }
        else{
            it.next();
            count++;
                      }
        }

2. 将我真+i写入文件

for(int i=0;i<10;i++){
    File file3=new File(manyfile,"helloworld"+i+".txt");
    file=new File(manyfile,"helloworld"+i+".txt");
    OutputStream os=null;
       try {
        os=new FileOutputStream(file3);
        String s="我真"+i;
        try {
            os.write(s.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
        try{
            os.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
}

3. Runnable和Thread的区别

public class FirstThread extends Thread {
        private int i; 
        public void run(){
            for(i=0;i<5;i++){
                System.out.println(getName()+" "+i);
            }
        }
        public static void main(String[] args) {
            for(int i=0;i<5;i++){
                System.out.println(Thread.currentThread().getName()+""+i);
                if(i==2){
                    new FirstThread().start();
                    new FirstThread().start();
                }
            }
        }
}
public class SecondThread implements Runnable {
    private int i;
    public void run() {
        for(i=0;i<5;i++){
            System.out.println(Thread.currentThread().getName()+""+i);
            if(i==2){
                SecondThread st=new SecondThread();
                new Thread(st,"新线程1").start();
                new Thread(st,"新线程2").start();
            }
        }
    }
}

Runnable和Thread的区别:
1,通过继承Thread类来创建线程,通过实现接口Runnable来创建线程类,一个类只能继承一个父 类,但可以实现多个接口,因此通过实现接口Runnable可以避免继承的局限性。
2,线程对象的创建:直接创建Thread子类即可代表线程对象;创建的Runable对象只能作为线程对象的target,多个线程可以共享同一个target,因此,实现接口Runnable适合于资源共享。
3,当线程类实现Runnable接口时,只能用Thread.currentThread()方法

@onlylemi onlylemi changed the title java1-杨娜1414011005 Java1-杨娜1414011005 Dec 24, 2015
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