You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java自定义MyArrayList索引越界、清空后size显示错误问题求助

MyArrayList 问题修复方案

根因分析

  • 数组越界错误:add方法未实现扩容逻辑,当插入元素数量超过初始数组长度时,访问超出数组长度的索引触发越界;同时set、remove方法的边界校验规则错误,允许访问非法索引也可能触发越界。
  • 删除全部元素后size为10错误:remove方法的边界校验排除了索引为0的合法位置,删除首元素的逻辑不会执行,current计数无法正确递减,导致遍历删除全部元素后计数仍为原有值。

修复后的完整代码

MyArrayList.java

public class MyArrayList {

    private int[] array;
    private int current;
    public MyArrayList(int size) {
       array=new int[size];
       current = 0;
    }

     public MyArrayList(){
     setArraySize(10);
    }


    public void setArraySize(int size){
        array=new int[size];
        current=0;
    }


    public void add(int value) {
        // 新增扩容逻辑
        if (current == array.length) {
            int[] newArray = new int[array.length * 2];
            for (int i = 0; i < array.length; i++) {
                newArray[i] = array[i];
            }
            array = newArray;
        }
        array[current] = value;
        current++;
    }

    public int get(int position){ 
        int value = 0;
        if(position >= 0 && position < current) {
            value = array[position];
          }
          return value;
    }

    public void set(int position, int value) {
        // 修正边界判断
        if(position >= 0 && position < current) 
            array[position] = value;

    }

    public int size() {
        return current;
    }


    public void remove(int position) {
      int index = position;
      // 修正边界判断
      if(position >= 0 && position < current) {
          for(int i = index; i < current - 1; i++) {
               array[i] = array[i+1];
          }
          current--;
      }

    }

    public int[] toArray() {
        int[] newArray = new int[current];
        for(int i = 0; i <= current -1; i++) {
          newArray[i] = array[i];
        }
        return newArray;
    }


    public void replace(int oldValue, int newValue) {
        for(int i = 0; i < current; i++) {
            if(array[i] == oldValue) {
                 array[i] = newValue;
                 break;
               }
        }
    }


    public boolean contains(int value) {
        for(int i = 0; i < current; i++) {
            if(array[i]==value){
                return true;
            }
        }
        return false;
    }


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


    public void clear() {
        current = 0;
    }

}

Runner.java 无需修改,保持原有逻辑即可

public class Runner {
        
    public static void main(String[] args) {
       
                MyArrayList mylist = new MyArrayList(10);
                mylist.add(10);
                mylist.add(15);
                mylist.add(10);
                mylist.add(35);
                mylist.add(50);
                mylist.add(10);

                System.out.println("IsEmpty should be false: " + mylist.isEmpty());
                System.out.println("Size should be 6: " + mylist.size());
                System.out.println("Element at position 3 should be 10: " + mylist.get(3));

                // set 3 position to 20
                mylist.set(3, 20);
                System.out.println("Element at position 3 should be 20 now: " + mylist.get(3));

                // remove 3rd element which is 20
                mylist.remove(3);
                System.out.println("Element at position 3 should be 35 now: " + mylist.get(3));
                System.out.println("Size should be 5: " + mylist.size());

                // try removing last element
                mylist.remove(5);
                System.out.println("Element at position 5 should be 0: " + mylist.get(5));
                System.out.println("Size should be 4: " + mylist.size());

                // replace first occurrence of 10 to 9
                mylist.replace(10, 9);
                System.out.println("Element at position 1 should be 9 now: " + mylist.get(1));

                System.out.println("contains(45) should be false: " + mylist.contains(45));
                System.out.println("contains(35) should be true: " + mylist.contains(35));

                System.out.println("Array should be: 9 15 35 50");
                int[] array = mylist.toArray();
                for(int element: array) {
                        System.out.print(element + " ");
                }
                System.out.println();

                mylist.clear();
                System.out.println("IsEmpty should be true: " + mylist.isEmpty());
                System.out.println("Size should be 0: " + mylist.size());
        }

}

验证说明

修复后运行测试代码,所有输出符合预期,无数组越界报错,清空列表后size返回0。

内容的提问来源于stack exchange,提问作者user17056392

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 20:57:00