数据结构--数组

数组基础

  1. 用来存储一组类型相同的数据
  2. 在内存中,分配连续的空间,数组创建时要指定容量(大小)
  3. 数据类型 [] 数组名  int [] arr = new int [10]       int [] arr2 = {1,2,3,4}
  4. 索引--访问数组时通过索引进行操作
  5. 索引从0开始,最大为arr.length-1
  6. 常见的错误:NullPointException   IndexOutOfBoundException
  7. 常见的数组:字符串,对象数组,哈希表

数组最大的优点:快速查询

数组最好应用于索引有语义的情况。

并非所有有语义的数字都可以作为数组的索引,例如:610521199610111188

数组也是可以处理“索引没有语义”的情况

package com.fs.lesson;



import java.util.Arrays;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Stream;

    /**
     * 创建自己的可变数组,基于Java中的数组
     */
    public class MyselfArray<T> {
        T[] data;// 数据容器
        int size; // 数组的大小

        public MyselfArray() {
            this(100);
        }

        public MyselfArray(int capacity) {
            this.data = (T[])new Object[capacity];
            this.size = 0;
        }

        // 判断数组是否为空
        public boolean isEmpty() {
            return this.size == 0;
        }

        // 获取数组中实际存放了多少个元素
        public int getSize() {
            return this.size;
        }

        // 获取容器
        public int getCapacity() {
            return this.data.length;
        }

        // 在尾部添加元素(size代表添加元素的索引)
        public void addElementTail(T element) {
            addElement(this.size, element);
        }

        // 在头部添加 (后面的元素进行后移)
        public void addElementFront(T element) {
            addElement(0, element);
        }

        // 在指定的位值添加元素
        public void addElement(int index, T element) {
            if (index < 0 || index > this.size) {
                throw new IllegalArgumentException("index is invalid!");
            }
            if (this.getSize() == this.getCapacity()) {
                // 扩容操作
                this.resize(2 * this.getCapacity());
            }

            //1、将index后的元素后移,包括index [index,this.size)
            int curIndex = this.size - 1;
            while (curIndex >= index) {
                this.data[curIndex + 1] = this.data[curIndex];
                curIndex -= 1;
            }
            //2、在索引为index的位置添加元素
            this.data[index] = element;
            //3、更新size
            this.size += 1;
        }

        // 获取指定位置的元素
        public Optional<T> getElementByIndex(int index) {
            if (index < 0 || index >= this.size) {
                return Optional.empty();
            }
            return Optional.of(this.data[index]);
        }

        // 修改指定位置的元素
        public void setElementByIndex(int index, T element) {
            if (index < 0 || index >= this.size) {
                throw new IllegalArgumentException("index is invalid!");
            }
            this.data[index] = element;
        }

        // 是否包含指定的元素
        public boolean isContainsElement(T element) {
            for (int i = 0; i < this.getSize(); i++) {
                if (this.data[i] == element) {
                    return true;
                }
            }
            return false;
        }

        // 从数组中获取指定元素的索引
        public int searchElementIndex(T element) {
            for (int i = 0; i < this.getSize(); i++) {
                if (this.data[i] == element) {
                    return i;
                }
            }
            return -1;
        }

        // 删除指定位置的元素
        public T removeElementByIndex(int index) {
            if (index < 0 || index >= this.size) {
                throw new IllegalArgumentException("index is invalid!");
            }
            T result = this.data[index];
            // i代表 填入位置的索引
            for (int i = index; i < this.getSize() - 1; i++) {
                this.data[i] = this.data[i + 1];
            }
            this.size -= 1;
            // 判断是否缩容(lazy)
            if (this.getCapacity() / 2 > 0 && this.size <= this.getCapacity() / 4) {
                resize(this.getCapacity() / 2);
            }
            return result;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder("[");
            for (int i = 0; i < this.size; i++) {
                sb.append(this.data[i]);
                if (i != this.size - 1) {
                    sb.append(",");
                }
            }
            sb.append("]");
            return sb.toString();
        }

        public void print(){
            Arrays.stream(this.data).forEach(System.out::println);
        }

        // 扩/缩容的方法
        private void resize(int newCapacity) {
            // 1、 创建一个新数组
            T[] newArr = (T[]) new Object[newCapacity];
            // 2、将原数组中的内容移动到新数组
            for (int i = 0; i < this.getSize(); i++) {
                newArr[i] = this.data[i];
            }
            // 3、让data容器指向新容器
            this.data = newArr;
        }


        public static void main(String[] args) {
            int capacity = 20;
            MyselfArray<Integer> array = new MyselfArray<>(capacity);
            Random random = new Random();
            for (int i = 0; i < capacity; i++) {
                array.addElementFront(random.nextInt(100));
                System.out.println(array);
            }
            System.out.println("实际存放元素的个数:" + array.getSize());
            for (int i = 0; i < 10; i++) {
                array.addElement(4, 50);
                System.out.println("实际存放元素的个数:" + array.getSize());
                System.out.println("数组的容积::" + array.getCapacity());
                array.removeElementByIndex(array.getSize() - 1);
                System.out.println("实际存放元素的个数:" + array.getSize());
                System.out.println("数组的容积::" + array.getCapacity());
            }
    /*    System.out.println(array);
        System.out.println("实际存放元素的个数:" + array.getSize());
        System.out.println(array.removeElementByIndex(0));
        System.out.println(array);
        System.out.println("实际存放元素的个数:" + array.getSize());
        System.out.println(array.removeElementByIndex(array.getSize()-1));
        System.out.println(array);
        System.out.println("实际存放元素的个数:" + array.getSize());
        System.out.println(array.removeElementByIndex(2));
        System.out.println(array);
        System.out.println("实际存放元素的个数:" + array.getSize());
         array.print();
        */
        }

    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值