数据结构之线性表顺序结构

知识点:1、确定线性表的基本操作:创建、插入、删除、查找.

           2、this的用法:http://www.cnblogs.com/java-class/archive/2012/12/19/2825463.html    

           3、运行时异常 :RunTimeException    http://blog.csdn.net/qq635785620/article/details/7781026

           4、泛型的理解:http://www.cnblogs.com/lwbqqyumidi/p/3837629.html

           5、构造函数   : http://longying2008.iteye.com/blog/1535722

 

 

import javax.management.RuntimeErrorException;

public class ArrayList<E> {
    Object[] arrays = null ;
    int current = 0;     //当前表长
    int capcity = 0;      //数组长度
    public ArrayList(){
        this(10);
    }
    
    //创建數組
    public ArrayList(int initalsize) {
    // TODO Auto-generated constructor stub
     if(initalsize<0){
         throw new RuntimeException("数组大小出现错误"+initalsize); //运行时错误!
     }else{
         this.arrays = new Object[initalsize];
         this.current = 0;
         this.capcity = initalsize;
     }
    }

//添加元素(在尾部)
    public void add(E e){
        isfull(capcity);
        this.arrays[current] = e;
        current ++;
    }
    //数据复制出来,再复制回去;保证数组够用,只能重新建立数据
    public void isfull(int cap){
        if(this.current == cap){
        this.capcity  = this.capcity+10;
        Object[] newarrays = new Object[capcity];
        for(int i = 0; i<cap ;i++){
               newarrays[i] = this.arrays[i];
        }
        this.arrays = newarrays;    
        }     
    }
    //根据下标号取出元素值
    public E getbyindex(int i){
        if(i>=0&&i<current)
            return (E) this.arrays[i];
        else  throw new RuntimeException("数组错误"+i);  
    }
    //插入一个元素,这里要提前判断一下数组大小
    public void insert(int i,E e){
        if(i<0||i>=this.capcity){
            throw new RuntimeException("下标号"+i+"出现错误!");
        }
        else{
            isfull(this.capcity);
            for(int j=this.current+1 ; j>=i ;j--){
                this.arrays[j] = this.arrays[j-1];
            }
            arrays[i] = e;
        }     
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<Integer> arr = new ArrayList<Integer>();
        arr.insert(9,2);
    }

}

 

转载于:https://www.cnblogs.com/neversayno/p/5065423.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值