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-韩锡勋1414010536 #22

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

Java1-韩锡勋1414010536 #22

hanxixun opened this issue Dec 24, 2015 · 0 comments

Comments

@hanxixun
Copy link

集合与泛型的应用实例

    int length=sheepList.size();
    Scanner reader=new Scanner(System.in);
    int number=reader.nextInt();
    //使用Collection的子类List,List有序的可重复
    ArrayList<Sheep> sheepList1=new ArrayList<sheep>();
    List<sheep>sheepList=sheepList1;
//遍历一遍羊的个数,因为是有序的所以有两种方法
//方法一:下标索引遍历
    for(int i=0;i<length;i++){
        System.out.println(i);
    }
//方法二:使用迭代器遍历删除
    Iterator<sheep>it=set.iterator();
    if(it.hasNext()){
        String s=it.next();
        it.remove();
        System.out.println("删除的元素为"+s);
    }
//删羊
//方法一:正常删除顺序,删除number个
    for(int i=0;i<number;i++){
        sheepList.remove(i);//因为List是有序的,所以可以使用下标索引
    }
//方法二:从后向前删number只羊
    for(int i=length;i>=length-number;i--){
        sheepList.remove(i);
    }

IO的基本使用与操作

//IO的普通操作
    FileInputStream fis=null;
    FileOutputStream fos=null;
    //IO异常是checkException,需要显性做出处理异常
    try{
        //生成输入流的对象
        fis=new FileInputStream("e:hello.txt");
        //生成输出流的对象
        fos=new FileOutputStream("e:hello1.txt");
        //生成一个字节数组
        byte[] buffer=new byte[1024];
        while(true){
            int temp=fis.read(buffer,0,buffer.length);
            if(temp==-1){
                break;
            }
            fos.write(buffer,0,temp);
        }
    catch(IOException e){
        System.out.println(e);
    }
    finallytry{//仍然是checkException,所以关闭时候也要捕获异常
            fis.close();
            fos.close();
        }
        catch{
            System.out.println(e);
        }
    }
//自动写文件
for(int i=0;i<9;i++){    //设置自动生成10个文件(0-9)
    file=new File(manyfile,"helloworld"+i+".txt");//manyfile为文件路径
    OutputStream out=null;
    try {
        out = new FileOutputStream(file);
        String s = "我是"+i;    //写入文件的内容
        try {
            out.write(s.getBytes());
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
    } 
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    finallytry{//仍然是checkException,所以关闭时候也要捕获异常
            out.close();
        }
        catch{
            System.out.println(e);
        }
    }
}       

线程的两种创建方法

//创建线程的方法一

    //定义一个线程类,它继承Thread并重新其中的run()方法。
    class FirstThread extends Thread {
        public void run(){
            for(int i=0;i <100;i++){
                System.out.println("FirstThread-->"+i);
            }
        }
    }

    public static void main(String args[]){
        //生成线程对象
        FirstThread firstThread=new FirstThread();
        //启动线程
        firstThread.start();

        //主方法也是一个线程
        for(int i=0;i <100;i++){
            System.out.println("main-->"+i);
        }
    }
//创建线程的方法二

    /*提供一个实现接口Runnable的类作为线程的目标对象,在初始化一个Thread类或者Thread子类的线程对象时,把目标
对象传递给这个线程的实例,由该目标对象提供线程体*/

    class SecondThread implements Runnable{
        public void run(){
            for(int i=0;i<100;i++){
                System.out.println("SecondThread-->"+i);
            }
        }
    }


    public static void main(String args[]){
        //生成一个Runnable接口实现类的对象
        SecondThread two =new SecondThread();
        //生成一个Thread对象,并将twoz作为参数传递给Thread对象
        Thread t=new Thread(two);
        //执行线程
        t.start();
    }
@hanxixun hanxixun changed the title Java-韩锡勋1414010536 Java1-韩锡勋1414010536 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