← 返回首页
数据结构与算法教程(Java版)-数组
发表时间:2021-09-11 21:04:11
数组

数组是最常见的线性结构,数组也是顺序表的一种实现方式,顺序表是在计算机内存中以数组的形式保存的线中以数组的形式保存的线性表。

关于数组的特征在JavaSE基础教程中有详细的阐述这里不再赘述,下面给出一个自己数组类以及常见的操作方法。

项目结构图如下:

Array.java

package com.simoniu.array;
/*
* 数组结构之顺序表实现-数组
* 自定数组类
* */
public class Array<E> {

    private E[] data;
    private int size;

    private Array(int capacity) {
        data = (E[]) new Object[capacity];
        size = 0;
    }

    //无参数的构造函数
    public Array() {
        //如果用户不传入capacity的话,就默认构建一个长度是10的数组。
        this(10);
    }

    //获取数组中的元素个数
    public int getSize() {
        return size;
    }

    //获取数组的容量
    public int getCapapcity() {
        return data.length;
    }

    //获取数组是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    //向所有元素后添加一个新元素
    public void addLast(E e) {
        if (size == data.length) {
            throw new IllegalArgumentException("failed;array is full");
        }
        //给size位置赋值。
        data[size] = e;
        //更新size的位置
        size++;
    }

    //在第index个位置插入元素e
    public void add(int index, E e) {
        if (size == data.length) {
            throw new IllegalArgumentException("failed;array is full");
        }
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("failed;index  >= 0 and  index <=size");
        }
        for (int i = size - 1; i >= index; i--) {
            data[i + 1] = data[i];
        }
        data[index] = e;
        size++;
    }

    //在所有元素前添加一个新元素添加元素
    public void addFirst(E e) {
        add(0, e);
    }

    //根据index索引位置获取元素
    public E get(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("failes,index is illegal");
        }
        return data[index];
    }

    //修改index处位置为e
    public void set(int index, E e) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("failes,index is illegal");
        }
        data[index] = e;
    }

    //查看元素中是否包含元素e
    public boolean contains(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e)) {
                return true;
            }
        }
        return false;
    }

    //查找数组中元素e所在的索引,如果不存在元素e,则返回-1
    public int find(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e)) {
                return i;
            }
        }
        return -1;
    }

    //从数组中删除index位置的元素,返回删除的元素
    public E remove(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("failes,index is illegal");
        }
        E ret = data[index];
        for (int i = index + 1; i < size; i++) {
            data[i - 1] = data[i];
        }
        size--;
        return ret;
    }

    //从数组中删除第一个元素,返回删除的元素
    public E removeFirst() {
        return remove(0);
    }

    //从数组中删除最后一个元素,返回删除元素。
    public E removeLast() {
        return remove(size - 1);
    }

    //从数组中删除元素e
    public boolean removeElement(E e) {
        int index = find(e);
        if (index != -1) {
            remove(index);
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("com.simoniu.array.Array: size = %d,capacity = %d \n", size, data.length));
        res.append("[");
        for (int i = 0; i < size; i++) {
            res.append(data[i]);
            if (i != size - 1) {
                res.append(", ");
            }
        }
        res.append("]");
        return res.toString();
    }

}

Students.java

package com.simoniu.array;

import java.util.Objects;

public class Students {

    private String name;
    private int score;

    public Students() {
    }

    public Students(String name, int score) {
        this.name = name;
        this.score = score;
    }

    @Override
    public String toString() {
        return String.format("Student(name:%s, score: %d)", name, score);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Students students = (Students) o;
        return score == students.score &&
                Objects.equals(name, students.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, score);
    }

    public static void main(String[] args) {

        Array<Students> array = new Array<>();
        array.addLast(new Students("张三", 89));
        array.addLast(new Students("李四", 66));
        array.addLast(new Students("王五", 88));
        System.out.println(array);

        System.out.println("学生数组是否为空:"+array.isEmpty());

        array.addFirst(new Students("张三丰", 100));
        System.out.println("-------添加张三丰-------");
        System.out.println(array);

        System.out.println("是否包含李四:"+array.contains(new Students("李四", 66)));
        array.removeElement(new Students("王五", 88));
        System.out.println("-------删除王五之后-------");
        System.out.println(array);
    }

}

运行结果:
com.simoniu.array.Array: size = 3,capacity = 10 
[Student(name:张三, score: 89), Student(name:李四, score: 66), Student(name:王五, score: 88)]
学生数组是否为空:false
-------添加张三丰-------
com.simoniu.array.Array: size = 4,capacity = 10 
[Student(name:张三丰, score: 100), Student(name:张三, score: 89), Student(name:李四, score: 66), Student(name:王五, score: 88)]
是否包含李四:true
-------删除王五之后-------
com.simoniu.array.Array: size = 3,capacity = 10 
[Student(name:张三丰, score: 100), Student(name:张三, score: 89), Student(name:李四, score: 66)]