• int 型动态数组


    main.java

    package com.company;
    //import java.util.ArrayList;
    
    public class Main {
    
        public static void main(String[] args) {
        // write your code here
            ArrayList list = new ArrayList();
            //list.get(-10);
            list.add(99);
            list.add(88);
            list.add(77);
            list.add(66);
            list.add(55);
    
            //list.remove(2);
            //list.get(5);
            //list.add(list.size(),100);
    
            //list.set(3,80);
            System.out.println(list);
    
    
    
    
    
    //        ArrayList list = new ArrayList(5);
    //        list.add(99);
    //        list.add(88);
    //        list.add(77);
    //        list.add(66);
    //        list.add(55);
    //        list.remove(2);
    //        System.out.println(list);
        }
    }
    
    
    

    ArrayList.java

    package com.company;
    
    import java.util.Arrays;
    
    public class ArrayList {
    
        private int size;
    
        private int[] elements;
    
        private static final int DEFALT_CAPACITY = 2;
        private static final int ELEMENT_NOT_FOUND = -1;
    
        public ArrayList(int capacity){
            capacity = (capacity < DEFALT_CAPACITY) ? DEFALT_CAPACITY : capacity;
            elements = new int[capacity];
    
        }
    
        public ArrayList(){
            //elements = new int[10];
            this(DEFALT_CAPACITY);
        }
    
        public int size(){
            return size;
        }
    
        public boolean isEmpty(){
            return size == 0;
        }
    
        /**
         * 获取index位置的元素
         * @param index
         * @return
         */
        public int get(int index){
            if (index < 0 || index >= size){
                throw new IndexOutOfBoundsException("index不合适");
            }
            return elements[index];
        }
    
        /**
         * 设置index位置的元素
         * @param index
         * @param element
         * @return
         */
        public int set(int index, int element){
            if (index < 0 || index >= size){
                throw new IndexOutOfBoundsException("index不合适");
            }
            int old = elements[index];
            elements[index] = element;
            return old;
        }
    
        /**
         * 某个元素的下标
         * @param element
         * @return
         */
        public int indexOf(int element){
            for (int i = 0; i < size; i++){
                if (elements[i] == element)return i;
            }
            return ELEMENT_NOT_FOUND;
        }
    
        /**
         * 是否包含某个元素
         * @param element
         * @return
         */
        public boolean contains(int element){
            return indexOf(element) != ELEMENT_NOT_FOUND;
        }
    
        public void add(int element){
            //elements[size++] = element;
            ensureCapacity(size+1);
           add(size,element);
        }
    
        public int remove(int index){
            System.out.println(size);
            if (index < 0 || index >= size){
                throw new IndexOutOfBoundsException("index不合适");
            }
            int old = elements[index];
            for (int i = index + 1; i <= size - 1; i++){
                elements[i - 1] = elements[i];
            }
            size--;
            return old;
        }
    
        public void clear(){
            size = 0;
        }
    
        /**
         * 往index位置添加元素 注意从后往前挪动
         * @param index
         * @param element
         */
        public void add(int index,int element){
            //允许=size
            if (index < 0 || index > size){
                throw new IndexOutOfBoundsException("index不合适");
            }
            for (int i = size - 1;i >= index;i--){
                elements[i+1] = elements[i];
            }
            elements[index] = element;
            size++;
        }
    
        /**
         * 保证要有capacity的容量
         * @param capacity
         */
        private void ensureCapacity(int capacity){
            int oldCapacity = elements.length;
            if (oldCapacity >= capacity)return;
            //动态扩容
            int newCapacity = oldCapacity + (oldCapacity >> 1);// 右移除以2
            System.out.println(size+"动态扩容"+newCapacity);
            int[] newElements = new int[newCapacity];
            for (int i = 0; i < size; i++){
                newElements[i] = elements[i];
            }
            elements = newElements;
        }
    
        @Override
        public String toString() {
    //这种会打印出全部位置的元素
    //        return "ArrayList{" +
    //                "size=" + size +
    //                ", elements=" + Arrays.toString(elements) +
    //                '}';
    
            StringBuilder string = new StringBuilder();
            string.append("size=").append(size).append(",[");
            for (int i = 0;i < size; i++){
                if (i != 0){
                    string.append(",");
                }
                string.append(elements[i]);
            }
            string.append("]");
            return string.toString();
    
    
        }
    }
    
    
    

    Assert.java

    package com.company;
    
    public class Assert {
        public static void test(boolean value){
            try{
                if(!value) throw new Exception("测试不通过");
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    
    
    
    此文仅为鄙人学习笔记之用,朋友你来了,如有不明白或者建议又或者想给我指点一二,请私信我。liuw_flexi@163.com/QQ群:582039935. 我的gitHub: (学习代码都在gitHub) https://github.com/nwgdegitHub/
  • 相关阅读:
    情报分析技术领域主要研究人员
    《Dynamic Topic Detection and Tracking: A Comparison of HDP, C-Word, and Cocitation Methods》笔记
    Adobe Acrobat 9 Pro 注册码
    文件访问被拒绝 需要管理员权限
    批量文件重命名工具-极力推荐 advanced renamer
    Discuz登录慢、退出也慢的原因?
    一些需要阅读的论文
    webview上传图片
    自定义圆形图片
    touch ImageView
  • 原文地址:https://www.cnblogs.com/liuw-flexi/p/13635626.html
Copyright © 2020-2023  润新知