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

知识点: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