RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
java手工实现ArrayList版本2

手工实现ArrayList第二版:
添加了数组扩容、返回索引元素、修改索引元素、删除、检查索引值、抛出异常、返回元素个数
尤其注意删除和扩容操作需要用到数组拷贝

成都创新互联服务项目包括勉县网站建设、勉县网站制作、勉县网页制作以及勉县网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,勉县网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到勉县省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

public class he {
    private int size;
    private static final int DEFAULT_CAPACITY=10;
    private Object[] ob;
    public he()//无参默认构造
    {
        ob=new Object[DEFAULT_CAPACITY];
    }
    public he(int capacity) //有参默认构造
    {
        ob=new Object[capacity];
    }

public void add(E obs)//添加元素和扩容
{
    if(size==ob.length)
    {
        Object[] newArray=new Object[ob.length+ob.length/2];
        System.arraycopy(ob,0, newArray, 0, ob.length);
        ob=newArray;
    }
    ob[size++]=obs;

}
public E get(int nums) //返回索引元素
{
    if(nums<0||nums>=size)
    {
        throw new RuntimeException("索引错误");
    }
    return (E)ob[nums];

}
public void set(int index,E obs)  //修改元素
{
    if( index<0||index>=size)
    {
        throw new RuntimeException("索引错误2");
    }
    ob[index]=obs;
}
public void checkIndex(int index)  //检查索引异常
    {
    if(index<0||index>=size)
        {
        throw new RuntimeException("索引错误3");
        }
}
public void rem(E obs)   //通过值移除
{
    for(int i=0;i=size)
    {
        throw new RuntimeException("索引错误4");
    }

    System.arraycopy(ob, index+1,ob, index, ob.length-1-index);
    ob[--size]=null;
}
public int size()  //返回元素个数
    {
    return size;
    }

public String toString()  //重写toString方法,直接打印数组名会打印地址
{
    StringBuilder s=new StringBuilder();
    s.append("[");
    for(int i=0;i h=new he<>(20);

    }

}


文章标题:java手工实现ArrayList版本2
文章来源:http://cqwzjz.cn/article/jhoecs.html