重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java手工实现ArrayList版本2-创新互联

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

10年积累的成都网站建设、网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有三元免费网站建设让你可以放心的选择与我们合作。
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);

    }

}

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


文章题目:java手工实现ArrayList版本2-创新互联
网页链接:http://cqcxhl.com/article/ecdoh.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP