一、数组

1.数组定义方式

数组存储的数据类型[]数组名字 = new 数组存储的数据类型[长度];

数据类型[] 数组名 = new 数据类型[]{元素1,元素2,元素3...};

数据类型[] 数组名 = {元素1,元素2,元素3...};(不能先声明后赋值,只能声明的同时赋值)

2.数组循环赋值,遍历打印

1587086449337

public class Main {
    public static void main(String[] args) {
        int[] arr = new int[10];
        for(int i = 0;i <arr.length;i++) //循环给数组赋值
            arr[i] = i;

        String[] scores = new String[]{"100","98","88"};
        for(String score:scores){
            System.out.println(score);
        }
    }
}

3.二次封装数组---》自定义动态数组

1587087102129

package dataStructure.arrays;


// 考虑到该数组适配所有类型,所以使用泛型
public class Array<T> {
    private int size;  //数组有效元素
    private T[] data; //


    public Array(int capacity) { // 用户传入数组容量大小
        this.data = (T[])new Object[capacity]; // 创建泛型对象时,先new父类,再强转
        this.size = 0;
    }

    public Array() {
        this(10);
    }

    public int getSize() { // 获取数组有效元素个数
        return this.size;
    }

    public int getCapacity() { // 查询容量
        return this.data.length;
    }

    public boolean isEmpty() { // 查询是否为空
        return size == 0;
    }

    public void insert(int index, T e) { // 向数组指定index插入一个元素e   O(n)
        if(index < 0 || index > size)
            throw new IllegalArgumentException("越界");

        if(data.length == size)
            this.resize(2 * this.data.length);  // 扩容2倍,eager扩容

        for (int i = size - 1; i >= index; i--)
            data[i + 1] = data[i];
        data[index] = e;
        size++;

    }

    public void addFirst(T e) {    // 队头插入元素    O(n)
        this.insert(0, e);
    }

    public void addLast(T e) { // 队尾插入元素      O(1)
        this.insert(this.size, e);
    }

    public void set(int index, T e) {   // 手动设置元素值     O(1)
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("越界");
        }
        this.data[index] = e;
    }

    public T get(int index) {   // 获取指定位置元素值
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("越界");
        }
        return data[index];
    }

    public boolean contains(T e) { // 数组中是否包含指定元素
        for (int i = 0; i < this.size; i++) {
            if (this.data[i].equals(e)) {
                return true;
            }
        }
        return false;
    }

    public int find(T e) {  // 返回指定元素的索引,如果没有返回-1
        for (int i = 0; i < this.size; i++) {
            if (this.data[i].equals(e)) {
                return i;
            }
        }
        return -1;
    }

    public T remove(int index) {  // 删除指定索引的元素    O(n)
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("");
        }
        T ret = this.data[index];
        for (int i = index; i < size-1; i++) {
            this.data[i] = this.data[i + 1];
        }
//        data[size] = null;     // 释放该引用,以便垃圾回收
        size--;
        if(size == data.length / 4 && data.length / 2 != 0){   // 当size为容量1/4时才缩小容量,lazy缩容,防止震荡
            this.resize(data.length / 2);   // 缩小容量
        }
        return ret;
    }

    public T removeFirst(){
        return this.remove(0);
    }

    public T removeLast(){
        return this.remove(this.size-1);
    }

    public boolean removeElement(T e){   // 删除指定元素并返回true,如果没有,返回false
        int index = this.find(e);
        if(index != -1){
            this.remove(index);
            return true;
        }
        return false;
    }



    @Override
    public String toString() {
        StringBuilder strb = new StringBuilder();
        strb.append(String.format("size:%d,capacity:%d,array:", this.size, this.getCapacity()));
        strb.append("[");
        for (int i = 0; i < size; i++) {
            strb.append(this.data[i]);
            if (i != size - 1) {
                strb.append(",");
            }
        }
        strb.append("]");
        return strb.toString();
    }

    private void resize(int newCapacity){       //     O(n)
        T[] newData = (T[])new Object[newCapacity];
        for(int i = 0;i<this.size;i++){
            newData[i] = data[i];
        }
        data = newData;

    }

}

package dataStructure.arrays;

public class Main {
    public static void main(String[] args) {
        Array<Character> data = new Array<>();
        for (Character ch = '0'; ch < '9'; ch++) {
            data.addLast(ch);
        }
        data.addLast('A');
        data.addLast('B');
        data.addFirst('#');
        data.removeLast();
        System.out.println(data);

        for (int i = 0; i < 9; i++) {
            data.removeLast();
            System.out.println(data);
        }
    }
}

size:11,capacity:20,array:[#,0,1,2,3,4,5,6,7,8,A]
size:10,capacity:20,array:[#,0,1,2,3,4,5,6,7,8]
size:9,capacity:20,array:[#,0,1,2,3,4,5,6,7]
size:8,capacity:20,array:[#,0,1,2,3,4,5,6]
size:7,capacity:20,array:[#,0,1,2,3,4,5]
size:6,capacity:20,array:[#,0,1,2,3,4]
size:5,capacity:10,array:[#,0,1,2,3]
size:4,capacity:10,array:[#,0,1,2]
size:3,capacity:10,array:[#,0,1]
size:2,capacity:5,array:[#,0]

4.时间复杂度分析

O(1), O(n), O(lgn), O(nlogn), O(n^2)

大O描述的是算法的运行时间和输入数据之间的关系,渐进时间复杂度

n趋近于无穷时,比较算法优劣

1587108607049

1587108869418

posted @ 2020-04-17 15:53  jacob_code  阅读(51)  评论(0)    收藏  举报