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

Illarion Kovalchuk picture Illarion Kovalchuk · Aug 3, 2010 · Viewed 74.2k times · Source

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?

Answer

Ricky Clarkson picture Ricky Clarkson · Aug 3, 2010
Arrays.asList(array).subList(x, y).

This method doesn't give you an array, but a List, which is far more flexible.