Skip to content

Java1-刘明俊 1407084212 #35

@1407084212lmj

Description

@1407084212lmj

删除后三只羊

int len=sheepList.size();
int count=0;
Iterator<Sheep> itr=sheepList.iterator();
while(itr.hasNext()){
    if(count>len-4){
        itr.next();
        itr.remove();
    }
    else {itr.next();
    count++;
    }
}

往文件中加

package cn.edu.nuc.NewClass4;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class test {

public static void main(String[] args) {
        // TODO Auto-generated method stub
        File file = new File("E:\\Android\\Eclipse_workspace\\NewClass4\\iotest");
        File file1 = new File("E:\\Android\\Eclipse_workspace\\NewClass4\\iotest\\1\\2");
        File file2 = new File("E:\\Android\\Eclipse_workspace\\NewClass4\\iotest\\helloworld.txt");
        File file3 = new File("E:\\Android\\Eclipse_workspace\\NewClass4\\iotest\\1\\2\\helloworld1.txt");
        File file4 = new File("E:\\Android\\Eclipse_workspace\\NewClass4\\iotest\\1\\2\\helloworld2.txt");
        File file5 = new File("E:\\Android\\Eclipse_workspace\\NewClass4\\iotest\\1\\2\\helloworld3.txt");
        File file6 = new File("E:\\Android\\Eclipse_workspace\\NewClass4\\iotest\\1\\2\\helloworld4.txt");
        try {
            file.mkdir();
            file1.mkdirs();
            file2.createNewFile();
            file3.createNewFile();
            file4.createNewFile();
            file5.createNewFile();
            file6.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String[] name=file.list();
        for(int i=0;i<name.length;i++){
            System.out.println(name[i]);
        }
        OutputStream IO = null;
        File[] Name=null;
        Name=file1.listFiles();
        System.out.println(Name.length);
        for(int i=0;i<Name.length;i++){
        try {
            IO=new FileOutputStream(Name[i], true);
            String s="中北大学安卓实验室";
            IO.write(s.getBytes());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }try {
            IO.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
    }
}

Java中Runnable和Thread的区别

package cn.edu.nuc.NewClass5;
public class test {
public static void main(String[] args) {
        // TODO Auto-generated method stub
        ThreadExtend H1=new ThreadExtend("一号");
        ThreadExtend H2=new ThreadExtend("二号");
        ThreadExtend H3=new ThreadExtend("三号");
        H1.start();
        H2.start();
        H3.start();
        ThreadImpl h=new ThreadImpl("一号");
        new Thread(h).start();
        new Thread(h).start();
        new Thread(h).start();
    }
}
class   ThreadImpl implements Runnable{
    private int number =10;
    private String name;
    public ThreadImpl(String name) {
        this.name = name;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("我是实现Runnable");
        for(int i=0;i<10;i++){
            System.out.println("Runnable线程:"+this.name+",i="+number--);
            if(number<1)
                break;
        }
    }
}
class   ThreadExtend extends Thread{
    private int number =10;
    public ThreadExtend(String name){
        super(name);
    }
    public ThreadExtend(){
        super();
    }
    public void run(){
        for(int i=0;i<10;i++){
            if(number<1)
                break;
            System.out.println("Thread线程:"+this.getName()+number--);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions