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 中获取数组的子数组?


基础教程推荐
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01