Small study notes: write your own array

1. Create a prototype array

This is a basic array, there are three easy method

public class Array {
    private int[] data;
    private int size;

    public Array(int capacity) {
        data = new int[capacity];
        size=0;
    }

    public Array() {
        this(10);
    }
    //查看数组内有几个元素
    public int getSize(){
        return size;
    }
    //查看数组是否为空
    public boolean isEmpty(){
        return size ==0;
    }
    //查看数组长度
    public int getCapaCity(){
        return data.length;
    }
}

Add elements to an array

Question: When we add an element in the final, it should be how to write methods?

answer:

//最后一位添加元素
    public void addList(int number){
        if (size == data.length ){
            throw new IllegalArgumentException("超出范围");
        }
        data[size] = number;
        size++;
    }
Question: When we want to add elements in the specified location, the method should be how to write?

Here Insert Picture Description
answer:

//在指定位置添加方法
    public void addIndex(int index,int number){
        if (size == data.length ){
            throw new IllegalArgumentException("数组超出范围");
        }
        if (index<0 || index>size ){
            throw new IllegalArgumentException("index超出范围");
        }
        for (int i = size-1;i>= index;i--){
            data[i+1]=data[i];
        }
        data[index]=number;
        size++;
    }

Query and modify elements in the array

Thoughts: We can not use System.out.println directly in view when the array elements () to print out, so we need to override toString method

answer

	@Override
    public String toString() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(String.format("size=%d,capacity=%d\n",size,data.length));
        stringBuffer.append("[");
        for (int i=0;i<size;i++){
            stringBuffer.append(data[i]);
            if (i!=size-1){
                stringBuffer.append(",");
            }
        }
        stringBuffer.append("]");
        return stringBuffer.toString();
    }
Let's test how we write array
public static void main(String[] args) {
        Array array = new Array(20);
        for (int i = 0; i <10 ; i++) {
            array.addList(i);
        }
        System.out.println(array);
    }

Print out the results to
Here Insert Picture Description
prove that the code we've written a success.

Here we can also remove the single element
int get(int index){
        if (index<0 || index>=size){
            throw new IllegalArgumentException("index超出范围");
        }
        return data[index];
    }
Here we can also modify a single element
void set(int index,int number){
        if (index<0 || index>=size){
            throw new IllegalArgumentException("index超出范围");
        }
        data[index]=number;
    }

The array contains, search and delete elements

Sometimes we need to check whether there is an element, so we can write a method.
//查看是否包含某个元素
   public boolean contains(int number){
       for (int i = 0; i <size ; i++) {
           if (data[i]==number){
               return true;
           }
       }
       return false;
   }
Not only do we inquire whether there is an element, but also query index of this element, so we can
 //查看是否包含某个元素的下标
   public int find(int number){
       for (int i = 0; i <size ; i++) {
           if (data[i]==number){
               return i;
           }
       }
       return -1;
   }
How do we remove elements from an array

Thoughts: When we delete an element, the array what happens? This change in the number of elements and add what kind of difference?
We can simulate it, create an array of length 6, and now we want to remove elements inside 3, can be used to cover after cover let a former
Here Insert Picture Description
code demonstrates

 public void delete(int index){
       if (size <0 ||size == data.length ){
           throw new IllegalArgumentException("数组超出范围");
       }
       if (index<0 || index>size ){
           throw new IllegalArgumentException("index超出范围");
       }
       for (int i=index+1;i<size;i++){
           data[i-1]=data[i];
       }
       size--;
   }

An array using generics

Now it is our turn into a generic array

public class Array<E> {
    private E[] data;
    private int size;

    public Array(int capacity) {
        data = (E[])new Object[capacity];
        size=0;
    }
   ....
   //因方法太多,省略下面的方法
}

Dynamic Array

Before our array of fixed length, but there are many times when we do not know is create an array of length created, so we want to become a dynamic array array

Specific implementation steps:
1. Create a new array
2, a circulating manner to copy the old elements of the array to the new array
3, and returns the new array

Specific implementation code
//生成动态数组
    private void resize(int newCapacity) {
        //1、创建一个新数组
        E[] newData = (E[])new Object[newCapacity];
        //2、用循环的方式把旧数组的元素复制到新数组上
        for (int i = 0; i <size ; i++) {
           newData[i]= data[i];
        }
        //3、返回新数组
        data=newData;
    }
Released three original articles · won praise 1 · views 109

Guess you like

Origin blog.csdn.net/weixin_45427658/article/details/104518569