我创建了一个基于数组的通用调整大小循环缓冲区,它在我的测试中表现良好,直到这种特殊情况
- 头 == 尾和
- 整个数组充满了元素
我希望数组像往常一样调整大小,把 head = 0 , tail = new position to insert ,一切都会好起来的。但这就是我得到的输出:
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)
可能是一些微小但重要的事情,但我似乎无法找到那可能是什么。有人可以帮忙吗?
注意:
head > 下一次出队发生的位置
tail > 下一次入队发生的位置
编辑:按照 ns47731 的建议,我将 arraycopy 行从 更改system.arraycopy(arr, head, tempArr, 0, size);
为System.arraycopy(arr, 0, tempArr, 0, size);
。这虽然解决了异常问题,但会导致逻辑错误,如下所示:
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() 本身,它是为线性数组设计的,而不是圆形数组,所以我为自己创建了一个简单的版本,可以与圆形数组一起使用:
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);
}
}
}
稍后我将不得不添加异常情况,但这通常有效。
修改后的代码
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
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