• 练习:自己写一个容器ArrayList集合 一一数组综合练习


      1 package cn.bjsxt.myCollection;
      2 
      3 import java.util.Arrays;
      4 
      5 /**
      6  * 天下文章一大抄,看你会抄不会抄。
      7  * 模拟Stringbuilder 写一个容器
      8  * 参照源码
      9  * 
     10  * 
     11  * @author Administrator
     12  *
     13  */
     14 public class MyArrayList {
     15     /**
     16      * The value is used for  object storage.
     17      */
     18     private Object[]value;
     19 
     20     /** 
     21      * The size is the number of characters used.
     22      */
     23     private int size;
     24     
     25     //查看size大小
     26     public int size(){
     27         return size;
     28     }
     29     //是否是空
     30     public boolean isEmpty(){
     31         return size==0;
     32     }
     33     
     34  //构造器
     35     public MyArrayList(){
     36         //value = new Object[16];
     37         this(10);
     38     }
     39     
     40     
     41     public MyArrayList(int size){
     42         if(size<0){
     43             
     44             try {
     45                 throw new Exception();
     46             } catch (Exception e) {
     47                 
     48                 e.printStackTrace();
     49             }
     50         }
     51         
     52         value = new Object[size];
     53     }
     54  
     55     //取出对象的方法 
     56     public Object get(int index) {
     57 //        //index范围[0,size-1]或者[0,index)一样
     58 //        if(index<0||index>size-1){
     59 //            
     60 //            try {
     61 //                throw new Exception();
     62 //            } catch (Exception e) {
     63 //                
     64 //                e.printStackTrace();
     65 //            }
     66 //            
     67 //        }
     68         rangeCheck(index);
     69         return value[index];
     70     }   
     71     
     72     //set
     73     public Object set(int index,Object object){
     74         //碰到 索引 都要判断    把下面代码封装成方法
     75 //           //index范围[0,size-1]或者[0,index)一样
     76 //        if(index<0||index>size-1){
     77 //            
     78 //            try {
     79 //                throw new Exception();
     80 //            } catch (Exception e) {
     81 //                
     82 //                e.printStackTrace();
     83 //            }
     84 //            
     85 //        }
     86         rangeCheck(index);
     87         Object old = value[index];
     88         value[index] = object;
     89         return old;
     90     }
     91     public  void rangeCheck(int index){
     92            //index范围[0,size-1]或者[0,index)一样
     93         if(index<0||index>size-1){
     94             
     95             try {
     96                 throw new Exception();
     97             } catch (Exception e) {
     98                 
     99                 e.printStackTrace();
    100             }
    101             
    102         }
    103     }
    104     
    105     //添加方法
    106     public void add(Object obj){
    107         value[size]=obj;
    108         size++;
    109         if(size>=value.length){
    110             //装不下了 扩容吧
    111             int newCapacity = value.length*2+1;
    112             Object[]newList = new Object[newCapacity];
    113             
    114             //System.arraycopy(src, srcPos, dest, destPos, length);
    115             
    116             for(int i=0;i<value.length;i++){
    117                 newList[i] = value[i];
    118             }
    119             value = newList;
    120         }
    121     }
    122     
    123     
    124     
    125     //查某个对象的下标值
    126     public int indexOf(Object obj){
    127         if(obj==null){
    128             return -1;
    129         }else{
    130             for(int i=0;i<value.length;i++){
    131                 if(obj==value[i]){
    132                     return i;
    133                 }
    134             }
    135             return -1;
    136         }
    137     }
    138     //倒着第一次出现某个对象的下标值
    139     public int lastIndexOf(Object obj){
    140         if(obj==null){
    141             return -1;
    142         }else{
    143             for(int i=value.length-1;i>=0;i--){
    144                 if(obj==value[i]){
    145                     return i;
    146             }
    147         }
    148             return -1;
    149     }
    150 }  
    151 
    152     public static void main(String[] args) {
    153         MyArrayList list = new MyArrayList(2);
    154         list.add("aaa");
    155         list.add(new Human("小高"));
    156         list.add("bbb");
    157         
    158         Human h=(Human)list.get(1);
    159         
    160         System.out.println(list.get(1));
    161         System.out.println(h.getName());
    162         /**
    163         *上面创建3个对象  你查第四个 不存在的
    164         *System.out.println(list.get(3));
    165         *越界异常
    166         *java.lang.Exception
    167         *    at cn.bjsxt.myCollection.MyArrayList.get(MyArrayList.java:62)             
    168         *    at cn.bjsxt.myCollection.MyArrayList.main(MyArrayList.java:82)
    169         */
    170         System.out.println(list.size());
    171     }
    172     
    173     
    174 }
  • 相关阅读:
    struts2.0中struts.xml配置文件详解
    spring配置文件详解
    web.xml讲解
    maven项目如何引用本地的jar包
    springmvc框架下ajax请求传参数中文乱码解决
    contentType设置类型导致ajax post data 获取不到数据
    spring 四种依赖注入方式以及注解注入方式
    eclipse 启动tomcat后 页面无法访问tomcat首页
    openfire安装完毕后无法登录控制台(忘记密码)的解决方法
    Mac下新安装的MySQL无法登陆root用户(安装时没有设置密码)
  • 原文地址:https://www.cnblogs.com/PoeticalJustice/p/7625810.html
Copyright © 2020-2023  润新知