首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >System.arraycopy()抛出越界异常

System.arraycopy()抛出越界异常
EN

Stack Overflow用户
提问于 2013-09-08 19:41:55
回答 1查看 9.6K关注 0票数 0

我创建了一个基于数组的通用大小循环缓冲区,它在我的测试中做得很好,直到在以下特定情况下

  1. 头部==尾部和
  2. 总数组由元素填充。

我预期数组将像往常一样调整大小,将head =0,tail =新位置插入,并且都会很好。但这就是我得到的输出:

代码语言:javascript
复制
adding : e
head : 5 tail : 5 , size : 12
a b c d e f g h i j k l 

adding : f
resizing array to size: 24
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at ResizingCircularArray.resize(ResizingCircularArray.java:16)
    at ResizingCircularArray.enqueue(ResizingCircularArray.java:33)
    at ResizingCircularArray.main(ResizingCircularArray.java:105)

可能是一件很有意义的事,但我似乎找不出那是什么。有人能帮忙吗?

Note

从下一个排队的位置出发

尾巴>位置进入下一次排队的位置

编辑:按照ns47731的建议,我把我的列印行从system.arraycopy(arr, head, tempArr, 0, size);改为System.arraycopy(arr, 0, tempArr, 0, size);。这虽然解决了异常问题,但会导致逻辑错误,如下所示:

代码语言:javascript
复制
43.dequeing : f
head : 13 tail : 20 , size : 7
null null null null null null null null null null null null null g h i j k l m null null null null 

44.dequeing : g
resizing array to size: 12
head : 0 tail : 6 , size : 6
null null null null null null null null null null null null 

数据部分g h i j k l m被删除了。

我意识到问题在于System.arraycopy()本身,它是为线性数组而不是循环数组设计的,所以我为自己创建了一个简单的版本,它可以用于循环数组:

代码语言:javascript
复制
private void arrayCopy(E[] srcArr , int srcpos , E[] destArr , int destpos , int length){
        for(int index = 0 ; index < length ; index++){
            destArr[index] = srcArr[head++];
            if(head == srcArr.length){
                head = (head % srcArr.length);
            }
        }
    }

稍后我将不得不添加例外情况,但这在一般情况下是可行的。

修改代码

代码语言:javascript
复制
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

class ResizingCircularArray<E> {

    private int head = 0;
    private int tail = 0;
    private int size = 0; // a measure of non-null elements in the array
    private E[] arr;

    // Modified version of System.arraycopy() to work with circular array.
    private void arrayCopy(E[] srcArr , int srcpos , E[] destArr , int destpos , int length){
        for(int index = 0 ; index < length ; index++){
            destArr[index] = srcArr[head++];
            if(head == srcArr.length){
                head = (head % srcArr.length);
            }
        }
    }

    private void resize() {
        System.out.println("resizing array to size: " + 2 * size);
        @SuppressWarnings("unchecked")
        E[] tempArr = (E[]) new Object[2 * size];
        arrayCopy(arr, head, tempArr, 0, size);
        head = 0;
        tail = size; // tail point to where the NEXT element will land
        arr = tempArr;
    }

    @SuppressWarnings("unchecked")
    public ResizingCircularArray() {
        arr = (E[]) new Object[3];

    }

    public void enqueue(E item) {
        if (item == null)
            throw new NullPointerException(
                    " adding null values is not allowed ");
        if (size == arr.length) {
            resize();
        }
        if (tail == arr.length) {
            // going round
            tail = (tail % arr.length);
        }
        arr[tail++] = item;
        size++;
        System.out.println("head : " + head + " tail : " + tail + " , size : "
                + size);
    }

    public E dequeue() {
        if (!(size > 0))
            throw new java.util.NoSuchElementException("size is negative");
        E item = arr[head];
        arr[head++] = null;
        if (head == (arr.length)) {
            head = (head % arr.length); // =0
        }
        --size;
        if (size == arr.length / 4) {
            resize();
        }
        System.out.println("head : " + head + " tail : " + tail + " , size : "
                + size);
        return item;
    }

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

    public E sample(int offset) {
        if (offset < 0)
            throw new java.lang.IllegalArgumentException(
                    "provided offset is out of bounds");
        return arr[head + offset];
        /*
         * NOTE : the check for (head+offset)>tail as pointed out by sos will
         * work in case of linear array , Not in case of Circular array because
         * when tail comes around in a circle , tail will be < than head and the
         * above check will create trouble
         */
    }

    public int size() {
        return size;
    }

    public void display() {
        for (E item : arr)
            System.out.print(item + " ");
        System.out.println("\n");
    }

    public static void main(String[] args) {

        ResizingCircularArray<String> r = new ResizingCircularArray<String>();
        String line = null;
        String[] segment, parsed;
        boolean endFlag = false;
        int count = 0;

        try (BufferedReader is = new BufferedReader(new FileReader(
                "CircArrayPoints.txt"))) {
            line = is.readLine();
            segment = line.trim().split(";");
            System.out.println("total commands : " + segment.length);
            for (int i = 0; !segment[i].equals("stop") && !endFlag; i++) {
                parsed = segment[i].split(" ");
                count++;
                switch (parsed[0]) {
                case "enq":
                    System.out.println(count+ ".adding : " + parsed[1]);
                    r.enqueue(parsed[1]);
                    r.display();
                    break;
                case "deq":
                    if (r.isEmpty()) {
                        System.out.println("Empty queue");
                        endFlag = true;
                        break;
                    }
                    // print after checking isEmpty() to make sure
                    // sample(0) doesn't call null etc
                    System.out.println(count+ ".dequeing : " + r.sample(0));
                    r.dequeue();
                    r.display();
                    break;
                case "disp":
                    r.display();
                    break;
                default:
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

文件: CircArrayPoints.txt

代码语言:javascript
复制
enq a;enq b;enq c;enq d;enq e;enq f;enq g;enq h;enq i;enq j;enq k;enq l;deq;deq;deq;deq;deq;enq a;enq b;enq c;enq d;enq e;enq f;enq g;enq h;enq i;enq j;enq k;enq l;enq m;deq;deq;deq;deq;deq;deq;deq;deq;deq;deq;deq;deq;deq;deq;deq;deq;deq;disp;stop
EN

回答 1

Stack Overflow用户

发布于 2013-09-08 19:56:07

让您的数组副本读

代码语言:javascript
复制
System.arraycopy(arr, 0, tempArr, 0, size);

为什么?参数#2 (int) startPos是从源数组开始复制的地方,您从数组的长度开始,导致索引超出界限。

有关该方法的更多细节,请查看http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#arraycopy(java.lang.Object%2C%20int%2C%20java.lang.Object%2C%20int%2C%20int)

编辑:首先,我解决了你的原始问题,你应该自己解决剩下的问题。阅读arraycopy (顺便说一下,它写得很好)。System.arraycopy(arr,head,tempArr,0,尾头);

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18687892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档