首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从数组数组中读取任意长度的数组

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

Stack Overflow用户
提问于 2016-08-22 08:03:42
回答 1查看 71关注 0票数 2

我正在努力编写一种算法,在不创建临时数组的情况下,从现有的数组中读取数组。

这是我的想法:

代码语言:javascript
运行
复制
[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. 

举个例子(pos = 12,length = 16):

代码语言:javascript
运行
复制
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ] // Skip this row as pos > row.length
[ ][ ][X][X][X][X][X]          // Read from position 2 (row.length - relative pos) until end of array
[X][X][X][X][X][X][X][X]       // Read entire array as length left > row.length
[X][X][X][ ][ ][ ][ ][ ][ ]    // Read from 0 to length left (aka 3)
[ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]

谢谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-22 21:54:53

在这里,我最终使用的实现是:

代码语言:javascript
运行
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39069898

复制
相关文章

相似问题

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