Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从数组数组中读取任意长度的数组_Java_Arrays_Algorithm - Fatal编程技术网

Java 从数组数组中读取任意长度的数组

Java 从数组数组中读取任意长度的数组,java,arrays,algorithm,Java,Arrays,Algorithm,我正在努力编写一个算法,从一个数组中读取一个数组,而不创建一个临时数组 以下是我的想法: [data correspond to a byte[][] that contains the data we want to read from] [each arrays can be of arbitrary sizes. No assumption can be done there] [limit is the sum of bytes that has been wrote to data b

我正在努力编写一个算法,从一个数组中读取一个数组,而不创建一个临时数组

以下是我的想法:

[data correspond to a byte[][] that contains the data we want to read from]
[each arrays can be of arbitrary sizes. No assumption can be done there]
[limit is the sum of bytes that has been wrote to data beforehand. (Appending an array of size X increment limit by X - 1)]
[position is the current position that has been read by previous calls to read(...)]

1) Skip until array that belongs to position is reached
2) Copy from saved position in the array to the output array
3) Move to next array
4) Copy as much bytes as we can into the output buffer
5) Repeat 3-4 until output buffer is filled with the proper amount of data or we've reached the end of the data. 
示例(位置=12,长度=16):


谢谢你的帮助

这里是我最后使用的实现:

public int read(byte[] into, int dstOffset, int length) {
    if (!isComplete()) {
        throw new IllegalStateException("Data is not complete yet");
    }

    int pointer = 0;
    int byteRead = 0;
    int total = 0;

    if (VERBOSE)
        Dog.d("Array size : " + data.length + " ID : " + id);
    for (int i = 0; i < data.length && (length > 0 || position >= limit); i++) {
        if (position > (pointer + data[i].length)) {
            pointer += data[i].length;
            if (VERBOSE)
                Dog.d("Skip -> I = " + i + ", Data length = " + data[i].length + ", Position = " + position + ", Pointer = " + pointer + ", Limit = " + limit);
        } else {
            if (VERBOSE)
                Dog.d("I = " + i + ", Data length = " + data[i].length + ", Position = " + position + ", Pointer = " + pointer + ", Limit = " + limit);
            // We are in the right array
            int srcOffset = position - pointer; // This is the starting position in our target array
            int relativeLimit = Math.min(limit - pointer, data[i].length); // The is the ending position in our target array

            byteRead = Math.min(relativeLimit - srcOffset, length); // Now the number is the byte read is either the number between start and end or the requested length

            if (VERBOSE)
                Dog.d("I = " + i + ", srcOffset = " + srcOffset + ", dstOffset = " + dstOffset + ", byteRead = " + byteRead + " out of " + data[i].length + " (limit is = " + limit + ", pointer = " + pointer + ")");
            System.arraycopy(data[i], srcOffset, into, dstOffset, byteRead);

            length -= byteRead; // We've read X bytes, so we still have to read (length - X)
            dstOffset += byteRead; // We need to start next write at previous position + X
            total += byteRead; // Counter to keep track of byte we've read

            pointer += data[i].length; // Current read position
            position += byteRead; // Current position
            if (VERBOSE)
                Dog.d("Left to write = " + length + ", byteRead = " + byteRead + ", total = " + total);
        }
    }

    if (VERBOSE)
        Dog.d("Wrote a total of :" + total);
    return total;
}
public int read(字节[]到,int-dstcoffset,int-length){
如果(!isComplete()){
抛出新的非法状态异常(“数据尚未完成”);
}
int指针=0;
int byteRead=0;
int-total=0;
如果(详细)
Dog.d(“数组大小:+data.length+”ID:+ID);
对于(int i=0;i0 | | position>=limit);i++){
if(位置>(指针+数据[i].长度)){
指针+=数据[i]。长度;
如果(详细)
Dog.d(“跳过->I=“+I+”,数据长度=“+Data[I].length+”,Position=“+Position+”,Pointer=“+Pointer+”,Limit=“+Limit”);
}否则{
如果(详细)
Dog.d(“I=“+I+”,数据长度=“+Data[I].length+”,Position=“+Position+”,Pointer=“+Pointer+”,Limit=“+Limit”);
//我们在正确的阵型中
int srcpoffset=position-pointer;//这是目标数组中的起始位置
int relativeLimit=Math.min(限制指针,数据[i].length);//是目标数组中的结束位置
byteRead=Math.min(relativeLimit-srcfostate,length);//现在这个数字是字节读取的开始和结束之间的数字,或者是请求的长度
如果(详细)
Dog.d(“+I=”+I+”,srcpoffset=“+srcpoffset+”,dstcoffset=“+dstcoffset+”,byteRead=“+byteRead+”)超出“+data[I]。长度+”(限制为=“+limit+”,指针=“+pointer+”);
System.arraycopy(数据[i]、srcOffset、into、dstOffset、byteRead);
length-=byteRead;//我们已经读取了X字节,所以我们仍然必须读取(length-X)
dstOffset+=byteRead;//我们需要在前一个位置+X开始下一次写入
total+=byteRead;//用于跟踪已读取字节的计数器
指针+=数据[i]。长度;//当前读取位置
position+=byteRead;//当前位置
如果(详细)
Dog.d(“左写=“+length+”,byteRead=“+byteRead+”,total=“+total”);
}
}
如果(详细)
d(“总共写了:“+总数”);
返回总数;
}

“似乎不起作用”你能解释一下吗?发生了什么?我的意思是,当我运行这个方法时,我没有输出正确的序列。但我在想,我想我搞砸了srcdoffset&position,因为指针不动,position-pointer给出了错误的值。
public int read(byte[] into, int dstOffset, int length) {
    if (!isComplete()) {
        throw new IllegalStateException("Data is not complete yet");
    }

    int pointer = 0;
    int byteRead = 0;
    int total = 0;

    if (VERBOSE)
        Dog.d("Array size : " + data.length + " ID : " + id);
    for (int i = 0; i < data.length && (length > 0 || position >= limit); i++) {
        if (position > (pointer + data[i].length)) {
            pointer += data[i].length;
            if (VERBOSE)
                Dog.d("Skip -> I = " + i + ", Data length = " + data[i].length + ", Position = " + position + ", Pointer = " + pointer + ", Limit = " + limit);
        } else {
            if (VERBOSE)
                Dog.d("I = " + i + ", Data length = " + data[i].length + ", Position = " + position + ", Pointer = " + pointer + ", Limit = " + limit);
            // We are in the right array
            int srcOffset = position - pointer; // This is the starting position in our target array
            int relativeLimit = Math.min(limit - pointer, data[i].length); // The is the ending position in our target array

            byteRead = Math.min(relativeLimit - srcOffset, length); // Now the number is the byte read is either the number between start and end or the requested length

            if (VERBOSE)
                Dog.d("I = " + i + ", srcOffset = " + srcOffset + ", dstOffset = " + dstOffset + ", byteRead = " + byteRead + " out of " + data[i].length + " (limit is = " + limit + ", pointer = " + pointer + ")");
            System.arraycopy(data[i], srcOffset, into, dstOffset, byteRead);

            length -= byteRead; // We've read X bytes, so we still have to read (length - X)
            dstOffset += byteRead; // We need to start next write at previous position + X
            total += byteRead; // Counter to keep track of byte we've read

            pointer += data[i].length; // Current read position
            position += byteRead; // Current position
            if (VERBOSE)
                Dog.d("Left to write = " + length + ", byteRead = " + byteRead + ", total = " + total);
        }
    }

    if (VERBOSE)
        Dog.d("Wrote a total of :" + total);
    return total;
}