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

数据结构之数组Array_JAVA源代码及注意事项

程序员文章站 2022-04-01 18:49:04
数据结构之数组Array_JAVA注意事项NO.1/*忘记加private!!!!!数组data和数组元素个数size外界不可访问要变为私有private*/private E[] data;private int size;NO.2/*构造函数的写法记住data = (E[]) new Object[Capacity]注意:对于多类型的E要进行类型的转换Objective------>E*/ public Array(int Capacity) {...

数据结构之数组Array_JAVA

注意事项

NO.1

/*忘记加private!!!!!
数组data和数组元素个数size外界不可访问
要变为私有private
*/
private E[] data;
private int size;

NO.2

/*构造函数的写法记住
data = (E[]) new Object[Capacity]
注意:对于多类型的E
要进行类型的转换Objective------>E
*/
    public Array(int Capacity) {
        data = (E[]) new Object[Capacity];
        size = 0;
    }

NO.3

    // 在index索引的位置插入一个新元素e
    public void insert(int index,E e){
        if(index < 0 || index > size)//   错误:size == getCapacity()!!!!!!!!!
            throw new IllegalArgumentException("insert is false");
        if(size == data.length)
            resize(2 * data.length);

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

//        for (int i = data.length; i > index; i--) {
//                data[i] = data[i - 1];
//        }这里写的不对,data.length!=size
        data[index]=e;
        size++;
    }

size:数组元素的个数
.length:数组的长度
Capacity:数组的容量=长度

NO.4

/*记住抛出异常的格式
*/
if(index < 0 || index >= size)
   throw new IllegalArgumentException("Set failed. Index is illegal.");

NO.5

/*重写toString方法的格式
*/
    @Override
    public String toString(){
        StringBuilder  res = new StringBuilder();

源代码

Array类

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

    // 构造函数,传入数组的容量capacity构造Array
    public Array(int Capacity) {
        data = (E[]) new Object[Capacity];
        size = 0;
    }
    // 无参数的构造函数,默认数组的容量capacity=10
     public Array(){
        this(10);
     }

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

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

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

    // 在index索引的位置插入一个新元素e
    public void insert(int index,E e){
        if(index < 0 || index > size)
            throw new IllegalArgumentException("insert is false");
        if(size == data.length)
            resize(2 * data.length);

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

        data[index]=e;
        size++;
    }

    // 向所有元素后添加一个新元素
    public void addLast(E e){
        insert(size,e);
    }

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

    // 获取index索引位置的元素
    public E get(int index){
        if(index < 0 || index >= size)
            throw new IllegalArgumentException("Get failed. Index is illegal.");
        return data[index];
    }
    // 修改index索引位置的元素为e
    public void set(int index,E e){
        if(index < 0 || index >= size)
            throw new IllegalArgumentException("Set failed. 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 delete(int index){
        if(index < 0 || index >= size)
            throw new IllegalArgumentException("Set failed. Index is illegal.");
        E temp = data[index];
        for(int i = index ; i < size - 1; i++){
                data[i]=data[i+1];
        }
        data[size-1]=null;
        size--;
        if(size == data.length / 4 && data.length / 2 != 0)
            resize(data.length / 2);
        return temp;
    }
    // 从数组中删除第一个元素, 返回删除的元素
    public E deleteFirst(){
        return delete(0);
    }

    // 从数组中删除最后一个元素, 返回删除的元素
    public E deleteLast(){
        return  delete(size-1);
    }
    // 从数组中删除元素e
    public void deleteElement(E e){
        int index=find(e);
        if(index!=-1)
        delete(index);
    }
    @Override
    public String toString(){
        StringBuilder  res = new StringBuilder();
        res.append(String.format("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();
    }

        // 将数组空间的容量变成newCapacity大小
        private void resize(int newCapacity){

            E[] newData = (E[])new Object[newCapacity];
            for(int i = 0 ; i < size ; i ++)
                newData[i] = data[i];
            data = newData;
        }
    }

主函数main

public class main {

    public static void main(String[] args) {

        Array<Integer> arr = new Array<>();
        for(int i = 0 ; i < 10 ; i ++)
            arr.addLast(i);
        System.out.println(arr);

        arr.insert(1, 100);
        System.out.println(arr);

        arr.addFirst(-1);
        System.out.println(arr);

        arr.delete(2);
        System.out.println(arr);

        arr.deleteElement(4);
        System.out.println(arr);

        arr.deleteFirst();
        System.out.println(arr);

        for(int i = 0 ; i < 4 ; i ++){
            arr.deleteFirst();
            System.out.println(arr);
        }
    }
}

结果

"C:\Program Files\Java\jdk14\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2020.2\lib\idea_rt.jar=64795:C:\Program Files\JetBrains\IntelliJ IDEA 2020.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\12778\IdeaProjects\first\out\production\Array com.Array.main
Array : size = 10 , capacity = 10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Array : size = 11 , capacity = 20
[0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Array : size = 12 , capacity = 20
[-1, 0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Array : size = 11 , capacity = 20
[-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Array : size = 10 , capacity = 20
[-1, 0, 1, 2, 3, 5, 6, 7, 8, 9]
Array : size = 9 , capacity = 20
[0, 1, 2, 3, 5, 6, 7, 8, 9]
Array : size = 8 , capacity = 20
[1, 2, 3, 5, 6, 7, 8, 9]
Array : size = 7 , capacity = 20
[2, 3, 5, 6, 7, 8, 9]
Array : size = 6 , capacity = 20
[3, 5, 6, 7, 8, 9]
Array : size = 5 , capacity = 10
[5, 6, 7, 8, 9]

Process finished with exit code 0

本文地址:https://blog.csdn.net/TOPic666/article/details/107700479

相关标签: 数据结构 java