How to get a sub array of array in Java, without copying data?

前端 未结 9 1899
执念已碎
执念已碎 2020-12-28 11:58

I have some library of classes, working with my data, which is being read into buffer. Is it possible somehow to avoid copying arrays again and again, passing parts of data

9条回答
  •  醉话见心
    2020-12-28 12:41

    You could take the same approach as the String class takes; create a class for immutable objects which are constructed from an array, a start offset and an end offset which offers access to the sub-array. The user of such an object does not have to know the distinction between the whole array or a sub-array. The constructor does not have to copy the array, just store the array reference and its boundaries.

提交回复
热议问题