zoukankan      html  css  js  c++  java
  • 第2章 不要小瞧数组

    2-1 使用Java中的数组

    2-2 二次封装属于我们自己的数组

    2-3 向数组中添加元素

    public class Array {
    
        private int[] data;
        private int size;
    
        // 构造函数,传入数组的容量capacity构造Array
        public Array(int capacity){
            data = new int[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;
        }
    
        // 向所有元素后添加一个新元素
        public void addLast(int e){
    
    //        if(size == data.length)
    //            throw new IllegalArgumentException("AddLast failed. Array is full.");
    //
    //        data[size] = e;
    //        size ++;
            add(size, e);
        }
    
        // 在所有元素前添加一个新元素
        public void addFirst(int e){
            add(0, e);
        }
    
        // 在index索引的位置插入一个新元素e
        public void add(int index, int e){
    
            if(size == data.length)
                throw new IllegalArgumentException("Add failed. Array is full.");
    
            if(index < 0 || index > size)
                throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");
    
            for(int i = size - 1; i >= index ; i --)
                data[i + 1] = data[i];
    
            data[index] = e;
    
            size ++;
        }
    
    }
    View Code

    2-4 数组中查询元素和修改元素 

    public class Array {
    
        private int[] data;
        private int size;
    
        // 构造函数,传入数组的容量capacity构造Array
        public Array(int capacity){
            data = new int[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;
        }
    
        // 向所有元素后添加一个新元素
        public void addLast(int e){
            add(size, e);
        }
    
        // 在所有元素前添加一个新元素
        public void addFirst(int e){
            add(0, e);
        }
    
        // 在index索引的位置插入一个新元素e
        public void add(int index, int e){
    
            if(size == data.length)
                throw new IllegalArgumentException("Add failed. Array is full.");
    
            if(index < 0 || index > size)
                throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");
    
            for(int i = size - 1; i >= index ; i --)
                data[i + 1] = data[i];
    
            data[index] = e;
    
            size ++;
        }
    
        // 获取index索引位置的元素
        public int 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, int e){
            if(index < 0 || index >= size)
                throw new IllegalArgumentException("Set failed. Index is illegal.");
            data[index] = e;
        }
    
        @Override
        public String toString(){
    
            StringBuilder res = new StringBuilder();
            res.append(String.format("Array: size = %d , capacity = %d
    ", 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();
        }
    }
    View Code

    2-5 包含,搜索和删除

    public class Array {
    
        private int[] data;
        private int size;
    
        // 构造函数,传入数组的容量capacity构造Array
        public Array(int capacity){
            data = new int[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;
        }
    
        // 向所有元素后添加一个新元素
        public void addLast(int e){
            add(size, e);
        }
    
        // 在所有元素前添加一个新元素
        public void addFirst(int e){
            add(0, e);
        }
    
        // 在index索引的位置插入一个新元素e
        public void add(int index, int e){
    
            if(size == data.length)
                throw new IllegalArgumentException("Add failed. Array is full.");
    
            if(index < 0 || index > size)
                throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");
    
            for(int i = size - 1; i >= index ; i --)
                data[i + 1] = data[i];
    
            data[index] = e;
    
            size ++;
        }
    
        // 获取index索引位置的元素
        public int 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, int e){
            if(index < 0 || index >= size)
                throw new IllegalArgumentException("Set failed. Index is illegal.");
            data[index] = e;
        }
    
        // 查找数组中是否有元素e
        public boolean contains(int e){
            for(int i = 0 ; i < size ; i ++){
                if(data[i] == e)
                    return true;
            }
            return false;
        }
    
        // 查找数组中元素e所在的索引,如果不存在元素e,则返回-1
        public int find(int e){
            for(int i = 0 ; i < size ; i ++){
                if(data[i] == e)
                    return i;
            }
            return -1;
        }
    
        // 从数组中删除index位置的元素, 返回删除的元素
        public int remove(int index){
            if(index < 0 || index >= size)
                throw new IllegalArgumentException("Remove failed. Index is illegal.");
    
            int ret = data[index];
            for(int i = index + 1 ; i < size ; i ++)
                data[i - 1] = data[i];
            size --;
            return ret;
        }
    
        // 从数组中删除第一个元素, 返回删除的元素
        public int removeFirst(){
            return remove(0);
        }
    
        // 从数组中删除最后一个元素, 返回删除的元素
        public int removeLast(){
            return remove(size - 1);
        }
    
        // 从数组中删除元素e
        public void removeElement(int e){
            int index = find(e);
            if(index != -1)
                remove(index);
        }
    
        @Override
        public String toString(){
    
            StringBuilder res = new StringBuilder();
            res.append(String.format("Array: size = %d , capacity = %d
    ", 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();
        }
    }
    View Code

     

    2-6 使用泛型

    public class Array {
    
        private int[] data;
        private int size;
    
        // 构造函数,传入数组的容量capacity构造Array
        public Array(int capacity){
            data = new int[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;
        }
    
        // 向所有元素后添加一个新元素
        public void addLast(int e){
            add(size, e);
        }
    
        // 在所有元素前添加一个新元素
        public void addFirst(int e){
            add(0, e);
        }
    
        // 在index索引的位置插入一个新元素e
        public void add(int index, int e){
    
            if(size == data.length)
                throw new IllegalArgumentException("Add failed. Array is full.");
    
            if(index < 0 || index > size)
                throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");
    
            for(int i = size - 1; i >= index ; i --)
                data[i + 1] = data[i];
    
            data[index] = e;
    
            size ++;
        }
    
        // 获取index索引位置的元素
        public int 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, int e){
            if(index < 0 || index >= size)
                throw new IllegalArgumentException("Set failed. Index is illegal.");
            data[index] = e;
        }
    
        // 查找数组中是否有元素e
        public boolean contains(int e){
            for(int i = 0 ; i < size ; i ++){
                if(data[i] == e)
                    return true;
            }
            return false;
        }
    
        // 查找数组中元素e所在的索引,如果不存在元素e,则返回-1
        public int find(int e){
            for(int i = 0 ; i < size ; i ++){
                if(data[i] == e)
                    return i;
            }
            return -1;
        }
    
        // 从数组中删除index位置的元素, 返回删除的元素
        public int remove(int index){
            if(index < 0 || index >= size)
                throw new IllegalArgumentException("Remove failed. Index is illegal.");
    
            int ret = data[index];
            for(int i = index + 1 ; i < size ; i ++)
                data[i - 1] = data[i];
            size --;
            return ret;
        }
    
        // 从数组中删除第一个元素, 返回删除的元素
        public int removeFirst(){
            return remove(0);
        }
    
        // 从数组中删除最后一个元素, 返回删除的元素
        public int removeLast(){
            return remove(size - 1);
        }
    
        // 从数组中删除元素e
        public void removeElement(int e){
            int index = find(e);
            if(index != -1)
                remove(index);
        }
    
        @Override
        public String toString(){
    
            StringBuilder res = new StringBuilder();
            res.append(String.format("Array: size = %d , capacity = %d
    ", 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();
        }
    }
    View Code

     

    2-7 动态数组

    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 add(int index, E e){
    
            if(index < 0 || index > size)
                throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");
    
            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){
            add(size, e);
        }
    
        // 在所有元素前添加一个新元素
        public void addFirst(E e){
            add(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 remove(int index){
            if(index < 0 || index >= size)
                throw new IllegalArgumentException("Remove failed. Index is illegal.");
    
            E ret = data[index];
            for(int i = index + 1 ; i < size ; i ++)
                data[i - 1] = data[i];
            size --;
            data[size] = null; // loitering objects != memory leak
    
            if(size == data.length / 2)
                resize(data.length / 2);
            return ret;
        }
    
        // 从数组中删除第一个元素, 返回删除的元素
        public E removeFirst(){
            return remove(0);
        }
    
        // 从数组中删除最后一个元素, 返回删除的元素
        public E removeLast(){
            return remove(size - 1);
        }
    
        // 从数组中删除元素e
        public void removeElement(E e){
            int index = find(e);
            if(index != -1)
                remove(index);
        }
    
        @Override
        public String toString(){
    
            StringBuilder res = new StringBuilder();
            res.append(String.format("Array: size = %d , capacity = %d
    ", 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;
        }
    }
    View Code

    2-8 简单的复杂度分析 

    大O算法复杂度速查表

     

    2-9 均摊复杂度和防止复杂度的震荡

     

    ===================================================

    end

    部分内容来自于学习编程期间收集于网络的免费分享资源和工作后购买的付费内容。
  • 相关阅读:
    [No0000CA]什么是“普瑞马法则”?以及 如何利用“普瑞马法则”,三步克服惰性
    [No0000B9]C# 类型基础 值类型和引用类型 及其 对象复制 浅度复制vs深度复制 深入研究2
    [No0000B8]WPF或Winform调用系统Console控制台显示信息
    [No0000B7]If else 与 三元表达式? : 效率对比
    [No0000B5]C# 类型基础 值类型和引用类型 及其 对象判等 深入研究1
    [No0000B4].Net中String是引用类型还是值类型,答string是特殊的引用类型
    [No0000B6]C#中 ==与equals的区别
    [No0000B3].NET C# 单体模式(Singleton)
    [No0000AF]去除wpf窗口标题栏ICON
    [No000090]C#捕获控制台(console)关闭事件及响应cmd快捷键
  • 原文地址:https://www.cnblogs.com/MarlonKang/p/12208152.html
Copyright © 2011-2022 走看看