用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

仿照JDK源码实现数组的增删功能

2017-11-22 作者: 筑梦少年举报

[java]代码库

package cn.bjsxt.array;
/**
 * 根据StringBuilder类的相关源码创建属于自己的数组类
 * 该类与jdk中的数组的区别在于该类中可以存储任何的对象
 * jdk中的数组只能存储char
 * @author 刘涛
 *
 */
public class MyArrayList {
    private Object[] value;//定义数组
    private int size;//数组的长度`
    public MyArrayList(){
        //无参构造器,默认初始化长度为16的数组
        //value = new Object[16];
        this(16);//此行代码和上面的代码含义相同,
    }
    public MyArrayList(int size){
        value = new Object[size];
    }
    public void add(Object obj){
        //因为默认的size初始化值为0,所以从0开始,然后自增会继续往后加
        value[size] = obj;
        size++;
        //对数组进行扩容,如果初始化数组的长度无法满足数组时便会进行自动的扩容操作
        if(size >= value.length){
            int newCapacity = size*2;
            Object[] newList = new Object[newCapacity];
            for(int i = 0; i < value.length;i++){
                newList[i] = value[i];
            }
            value = newList;
        }
    }
    public Object get(int index){
        if(index < 0 || index >= size){
            try {
                throw new Exception();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return value[index];
    }
    /**
     * 删除数组中指定位置的元素,采取方式为数组中的最后一个元素替换将要删除的元素的
     * @param index 要删除的元素的位置
     */
    public void deleteCharAt(int index){
        if (index < 0 || index > size) {
            try {
                throw new Exception();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            //将数组中的最后一个元素替换删除元素的即可
            value[index] = value[size - 1];
            //删除后数组的长度减1
            size --;
            //创建一个新的数组数组的长度为原来数组的长度减1,并将原来的数组中的元素复制到新数组中
            Object[] newList = new Object[size];
            for(int i = 0; i < size; i++){
                newList[i] = value[i];
            }
            //System.arraycopy(value, 0, newList, 0, size);//将原始数组中的数据复制到新数组中的另一种方法
              value = newList;
        }
    }
    /**
     * 删除数组中指定位置的元素,删除方式为删除指定位置的元素后,后面的元素向前移动
     * @param index
     */
    public void removeCharAt(int index){
        //判断删除元素的位置是否在数组的长度范围内,若不在范围内则抛出异常
        if (index < 0 || index > size) {
            try {
                throw new Exception();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            for(int i = 0; i < size; i ++){
                value[i + index] = value[i + index + 1];
            }
            size --;
            Object[] newList = new Object[size];
            for(int i = 0; i < size; i ++){
                newList[i] = value[i];
            }
            //System.arraycopy(value, 0, newList, 0, size);//将原始数组中的数据复制到新数组中的另一种方法
            value = newList;
        }
    }
    public void insertCharAt(int index,Object obj){
        if (index < 0 || index > size ) {
            try {
                throw new Exception();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            value[size] = obj;
            size++;
            Object[] newList = new Object[size];
            System.arraycopy(value, 0, newList, 0, size);
            value = newList;
        }
    }
    /***
     * 该方法还需要进行调整 目前还无法实现
     * @param index
     * @param obj
     */
    public void insert(int index,Object obj){
        if (index < 0 || index > size ) {
            try {
                throw new Exception();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            size++;
            for(int i = 0;i < size; i ++){
                System.out.println(i);
                if((index + i + 1) > size){
                    value[index + i + 1] = null;
                    return;
                }else{
                    value[index + 2 + i] = value[index + i + 1];
                    value[index] = obj;
                }
            }
            Object[] newList = new Object[size];
            System.arraycopy(value, 0, newList, 0, size);
            value = newList;
        }
    }
    /**
     * 删除数组中指定区间的元素
     * @param start 区间起始位置
     * @param end 区间结束位置
     */
    public void remove(int start,int end){
        if(start < 0 || end > size || end < 0 || start > size){
            try {
                throw new Exception();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else{
            for(int i = 0; i < size; i ++){
                if(i+end > size){
                    value[i + start] = null;
                }else{
                    value[i + start] = value[i + end];
                }
            }
            size -= end - start;
            Object[] newList = new Object[size];
            for(int i = 0 ; i < size; i ++){
                newList[i] = value[i];
            }
            //System.arraycopy(value, 0, newList, 0, size);//将原始数组中的数据复制到新数组中的另一种方法
            value = newList;
        }
    }
    public static void main(String[] args) {
        MyArrayList my = new MyArrayList(2);
        my.add("aaa");
        my.add("bbb");
        my.add("ccc");
        my.add(new Human("孙悟空",500,true));
//      Human h = (Human) my.get(3);
//      System.out.println(h.getName());
//      System.out.println(h.isGender());
//      System.out.println(h.getAge());
//      System.out.println(my.get(2));
        System.out.println(my.size);
//      my.insertCharAt(3, "eee");
//      my.insert(4, "fff");
        System.out.println();
//      my.deleteChar(1);
//      my.removeCharAt(1);
//      my.remove(1, 3);
        my.insert(3, "fff");
        for(int i = 0; i < my.size; i ++){
            System.out.println(my.get(i));
        }
        System.out.println(my.size);
        /*System.out.println(my.size);
        Human h1 = (Human) my.get(1);
        System.out.println(h1.getAge());*/
    }
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...