我的java学习日记(12)

java学习第十二节之集合

一、           数组容器的优化

  publicclass Vector {

      privateObject[] objs = null;

      privateint index = 0;

   //写一个addAll方法,添加数组

      publicvoid addAll(Object[] objs) {

             for(Object object : objs) {

                    add(object);

             }

      }

   //写一个add方法,给数组添加元素

      publicvoid add(Object obj) {

             if(objs == null) {

                    objs= new Object[5];

                    objs[0]= obj;

                    index++;

             }else {

        //下边该方法优于for循环

                    if(index == objs.length) {

                           Object[]objs1 = new Object[(int) (objs.length * 1.5)];

                           System.arraycopy(objs,0, objs1, 0, objs.length);

                           objs= objs1;

                    }

                    objs[index]= obj;

                    index++;

             }

      }

   //写一个size方法,返回数组下标

      publicint size() {

             returnindex;

      }

   //一个get方法,获得数组下标

      publicObject get(int index) throws IndexOutOfBoundsException {

             if(index >= this.index) {

                    thrownew IndexOutOfBoundsException(index + "");

             }

             returnobjs[index];

      }

    //remove方法,删除第i个元素

      publicvoid remove(int i) {

             if(i >= index || i < 0) {

                    thrownew IndexOutOfBoundsException(index + "");

             }

             //Object objs1=new Object();

             System.arraycopy(objs,i + 1, objs, i, objs.length - i - 1);

             index--;

      }

}

二、           Scanner的使用

  importjava.util.Scanner;

public class Test {

      publicstatic void main(String[] args) {

             Scanners = new Scanner(System.in);             

             //int index=s.nextInt();

             //System.out.println(index);

 

             Stringline = s.nextLine();

             System.out.println(line);

      }

 }

三、           简易ATM

大概思路是创建一个AtmIf接口,里面的方法有取钱、存钱、查询,并用AtmIm类实现。本人正在研究,有想法的欢迎交流下。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章