int数组操作类(不是list,自动扩张的数组)

public class OptIntArrayList {
    private int[] elements;
    private int size;
    private boolean dummy;

    public static OptIntArrayList DUMMY = new OptIntArrayList(0, true);

    public static String OUT_OF_SIZE;

    public OptIntArrayList() {
        this(16);
    }

    public OptIntArrayList(int capacity) {
        this.elements = new int[capacity];
        this.size = 0;
    }

    public OptIntArrayList(int capacity, boolean dummy) {
        this.elements = new int[capacity];
        this.size = 0;
        this.dummy = dummy;
    }

    public OptIntArrayList(int capacity, int size) {
        this.elements = new int[capacity];
        this.size = size;
        assert size <= capacity;
    }

    public OptIntArrayList(int[] values) {
        this.size = values.length;
        this.elements = new int[size];
        System.arraycopy(values, 0, elements, 0, size);
    }

    public OptIntArrayList(int[] values, int size) {
        this.size = size;
        this.elements = new int[size];
        System.arraycopy(values, 0, elements, 0, size);
    }

    public OptIntArrayList(int[] values, int index, int size) {
        this.size = size;
        this.elements = new int[size];
        System.arraycopy(values, index, elements, 0, size);
    }

    public OptIntArrayList(OptIntArrayList another) {
        this.size = another.size;
        this.elements = new int[size];
        System.arraycopy(another.elements, 0, elements, 0, size);
    }

    public static OptIntArrayList wrap(int[] values) {
        OptIntArrayList list = new OptIntArrayList();
        list.elements = values;
        list.size = values.length;
        return list;
    }

    public static OptIntArrayList wrap(int[] values, int size) {
        OptIntArrayList list = new OptIntArrayList();
        list.elements = values;
        list.size = size;
        return list;
    }

    public int[] toIntArray() {
        int[] copyElements = new int[size];
        System.arraycopy(elements, 0, copyElements, 0, size);
        return copyElements;
    }

    public boolean isDummy() {
        return dummy;
    }

    public int getInt(int index) {
        return elements[index];
    }

    public int get(int index) {
        return elements[index];
    }

    public int removeInt(int index) {
        // check index to see whether it's legal
        if (index >= size) {
            // can not be larger than size
            throw new IndexOutOfBoundsException(OUT_OF_SIZE);
        }
        int old = elements[index];
        size--;
        if(index != size) {
            System.arraycopy(elements, index + 1, elements, index, size - index);
        }
        return old;        
    }

    public int[] elements() {
        return elements;
    }

    public int set(int index, int newValue) {
        // check index to see whether it's legal
        if (index >= size) {
            // can not be larger than size
            throw new IndexOutOfBoundsException(OUT_OF_SIZE);
        }
        int oldValue = elements[index];
        elements[index] = newValue;
        return oldValue;
    }

    public int size() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int capacity() {
        return elements.length;
    }

    public void add(int index, int value) {
        // check index to see whether it's legal
        if (index > size) {
            // can not be larger than size
            throw new IndexOutOfBoundsException(OUT_OF_SIZE);
        }
        if (size == elements.length) {
            int oldLength = size;
            int newLength = Math.max(size + 1,  (size + (size >> 1)));
            if (newLength == 0) {
                newLength = 1;
            }
            if (newLength == oldLength) {
                newLength = oldLength + 1;
            }
            int[] newElements = new int[newLength];
            System.arraycopy(elements, 0, newElements, 0, size);
            this.elements = newElements;
        } 
        if(index != size) {
            System.arraycopy(elements, index, elements, index + 1, size - index);
        }
        elements[index] = value;       
        size++;
    }

    public boolean add(int value) {
//        add(size, value);
        if (size == elements.length) {
            int oldLength = size;
            int newLength = Math.max(size + 1,  (size + (size >> 1)));
            if (newLength == 0) {
                newLength = 1;
            }
            if (newLength == oldLength) {
                newLength = oldLength + 1;
            }
            int[] newElements = new int[newLength];
            System.arraycopy(elements, 0, newElements, 0, size);
            this.elements = newElements;
        } 
        elements[size] = value;       
        size++;
        return true;
    }

    public void addNulls(int addSize) {
        int newSize = size + addSize;
        if (newSize > elements.length) {
            int oldLength = size;
            int newLength = Math.max(size + addSize,  (size + (size >> 1)));
            if (newLength == 0) {
                newLength = 1;
            }
            if (newLength == oldLength) {
                newLength = oldLength + addSize;
            }
            int[] newElements = new int[newLength];
            System.arraycopy(elements, 0, newElements, 0, size);
            this.elements = newElements;
        } 
        size = newSize;
    }

    public void addAll(int index, OptIntArrayList another) {
        int addSize = another.size();
        addElements(index, another.elements, 0, addSize);
    }

    public void addAll(OptIntArrayList another) {
        int addSize = another.size();
        addElements(size, another.elements, 0, addSize);
    }

    public void addElements(int index, int[] addElements, int offset, int length) {
        int newSize = size + length;
        if (newSize > elements.length) {
            int newLength = Math.max(newSize, (size + (size >> 1)));
            int[] newElements = new int[newLength];
            System.arraycopy(elements, 0, newElements, 0, size);
            this.elements = newElements;
        }
        if(index != size) {
            System.arraycopy(elements, index, elements, index + length, size - index);
        }
        System.arraycopy(addElements, offset, elements, index, length); 
        this.size = newSize;        
    }

    public int remove(int index)
    {
        return removeInt(index);
    }    

    public void trim() {
        int elementsSize = elements.length;
        if (size == elementsSize) {
            // no need to trim
            return;
        }
        int[] newElements = new int[size];
        // make a copy
        System.arraycopy(elements, 0, newElements, 0, size);
        // assign
        this.elements = newElements;
    }

    public void reset() {
        this.size = 0;
    }

    public void clear() {
        this.elements = new int[16];
        this.size = 0;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public int indexOf(int o) {
        for (int i = 0; i < size; i++) {
            if (Functions.equals(o, elements[i])) {
                return i;
            }
        }
    return -1;
    }

    public String toString() {
        StringBuilder s = new StringBuilder();
        s.append("[");
        for (int i = 0; i < size; i++) {
            if(i > 0) {
                s.append(", ");
            }
            int e = elements[i];
            s.append("" + e);
        }
        s.append("]");
        return s.toString();
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本
可以使用多种方法将int数组转换为List。一种方法是使用Java 8的流操作。首先,将int数组转换为数值流,然后将流中的元素装箱为Integer型,最后将流转换为List。代码示例如下: ```java int\[\] nums = {1, 2, 3, 4, 5}; List<Integer> list = Arrays.stream(nums) .boxed() .collect(Collectors.toList()); ``` 另一种方法是使用传统的for循环遍历int数组,并将每个元素添加到List中。代码示例如下: ```java int\[\] nums = {1, 2, 3, 4, 5}; List<Integer> list = new ArrayList<>(); for (int element : nums) { list.add(element); } ``` 无论使用哪种方法,最终都可以将int数组成功转换为List。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *3* [将int数组转为List集合](https://blog.csdn.net/lutrra/article/details/123101123)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [java int数组如何转换为List集合](https://blog.csdn.net/weixin_44712870/article/details/123910160)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热爱Coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值