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 deeper and deeper into processing methods? Well, it sounds strange, but in my particular case, there's a special writer, which divides data into blocks and writes them individually into different locations, so it just performs System.arraycopy, gets what it needs and calls underlying writer, with that new sub array. And this happens many times. What is the best approach to refactor such code?
From javadoc: "Copies the specified range of the specified array into a new array", which is not what the OP wants.
f1sh
2010-08-03 11:28:46
+1
A:
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.
rsp
2010-08-03 10:35:12
+6
A:
Many classes in Java accept a subset of an arrays as parameter. E.g. Writer.write(char cbuf[], int off, int len). Maybe this already suffices for your usecase.
Markus Kull
2010-08-03 10:42:45
+1
A:
Arrays.asList(array).subList(x, y).
It doesn't give you an array, but a List, which is far more flexible.
Ricky Clarkson
2010-08-03 11:25:46