How to get a sub array of array in Java, without copying data?(如何在不复制数据的情况下在 Java 中获取数组的子数组?)
问题描述
我有一些类库,正在处理我的数据,这些数据正在读入缓冲区.是否有可能以某种方式避免一次又一次地复制数组,将部分数据越来越深地传递到处理方法中?好吧,这听起来很奇怪,但在我的特殊情况下,有一个特殊的编写器,它将数据分成块并将它们单独写入不同的位置,因此它只执行 System.arraycopy,获取它需要的内容并调用底层编写器,使用那个新的子数组.这种情况发生了很多次.重构此类代码的最佳方法是什么?
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?
推荐答案
Java 中的许多类都接受数组的子集作为参数.例如.Writer.write(char cbuf[], int off, int len).也许这对于您的用例已经足够了.
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.
这篇关于如何在不复制数据的情况下在 Java 中获取数组的子数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在不复制数据的情况下在 Java 中获取数组的子数组?


- 如何指定 CORS 的响应标头? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 转换 ldap 日期 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 获取数字的最后一位 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01