欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Arrlist的重要方法重写

程序员文章站 2022-04-24 10:10:58
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 ArrLis... ......
  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     }