Arrlist的重要方法重写

2018-09-10 01:02:48来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

  1 import java.util.Arrays;
  2 
  3 public class ArrayOperator {
  4 
  5     public static void main(String[] args) {
  6         // TODO Auto-generated method stub
  7  ArrList list = new ArrList();
  8   list.add(5);
  9   list.add(7);
 10   list.add(6);
 11   System.out.println(list.sizeof());
 12 //  list.remove(0);
 13 list.insert(1, 7);
 14 System.out.println(list.toString());
 15 System.out.println(list.search(6));
 16   
 17     }
 18 
 19 }
 20 class ArrList{
 21     //定义元素个数
 22     private int size =  0;
 23     //初始定义数组
 24     private int[] data = new int[10];
 25     public ArrList(){
 26     }
 27     public ArrList(int inificapacity){ //初始化数组容量的构造方法
 28         if(inificapacity < 0){
 29             throw new ArrayIndexOutOfBoundsException();
 30         }else {
 31             data = new int[inificapacity];
 32         }
 33     }
 34     public boolean isEmpty(){ //判断数组是否为空操作
 35         return size ==0;
 36     }
 37     public  void grow(){ //判断元素是否需要扩容
 38         if(data.length <=1){
 39             data = Arrays.copyOf(data, data.length+1);
 40         }else{
 41             data = Arrays.copyOf(data, data.length+data.length>>1);
 42         }
 43         
 44     }
 45     public void add(int element){ //增加元素操作
 46         if(size >= data.length){
 47             this.grow();
 48         }else{
 49             data[size++] = element;
 50         }
 51     }
 52     public  void insert(int index,int element){ //插入元素操作
 53         if(index < 0){
 54             throw new ArrayIndexOutOfBoundsException();
 55         }else if(index <= size){
 56             for(int i = size-1; i >= index;i--){
 57                  data[i+1] = data[i];
 58             } data[index] = element;
 59             size++;
 60             
 61         }else{
 62             throw new ArrayIndexOutOfBoundsException();
 63         }
 64     }
 65     public  void remove(int index){
 66         if(index < 0){
 67             throw new ArrayIndexOutOfBoundsException();
 68         }
 69         else if(index < size){
 70             for(int i = index; i < data.length-1;i++){
 71                 data[i] = data[i+1];
 72             }size--;
 73         }else{
 74             throw new ArrayIndexOutOfBoundsException();
 75         }
 76     }
 77     public int search(int element){
 78         
 79         for(int i = 0; i < size;i++){
 80             if(data[i] == element){
 81                 
 82                 return i;
 83             }
 84         }
 85         return -1;
 86     }
 87     public  int ele(int index){
 88         if(index < 0 || index >= size){
 89             throw new ArrayIndexOutOfBoundsException();
 90         }else{
 91             return data[index];
 92         }
 93     }
 94     public  int sizeof(){
 95         return size;
 96     }
 97     public  void clear(){
 98         data = new int[10];
 99         size = 0;
100     }
101     @Override
102     public String toString(){
103         StringBuilder str = new StringBuilder("[");
104         for(int i = 0 ;i < size ;i++){
105             str.append(data[i]).append(",");
106         }
107         String sb = str.toString();
108         if(size > 0){
109             sb= sb.substring(0,sb.length()-1);
110         }
111         return sb + "]";
112     }
113     }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:并发执行任务

下一篇:并发工具