Java 截断一个数组而不复制它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1074295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Truncate an array without copying it?
提问by Rob
In Java, is there a way to truncate an array without having to make a copy of it? The common idiom is Arrays.copyOf(foo, n)
(where the new array is n elements long). I don't think there is an alternative, but I'm curious as to whether there is a better approach.
在 Java 中,有没有办法截断数组而不必复制它?常见的习惯用法是Arrays.copyOf(foo, n)
(其中新数组的长度为 n 个元素)。我不认为有替代方案,但我很好奇是否有更好的方法。
采纳答案by AlbertoPL
An array's length in Java cannot be altered after initialization, so you're forced to make a copy with the new size. Actually, the length parameter of a Java array is declared as final, so it cannot be changed once it's set.
Java 中数组的长度在初始化后无法更改,因此您必须使用新的大小进行复制。实际上,Java 数组的长度参数声明为 final,因此一旦设置就无法更改。
If you need to change an array's size, I'd use an ArrayList.
如果您需要更改数组的大小,我会使用 ArrayList。
回答by David Johnstone
I don't believe so. An array is allocated as a contiguous block of memory, and I can't imagine that there is any way of releasing a part of that block.
我不相信。数组被分配为连续的内存块,我无法想象有任何方法可以释放该块的一部分。
回答by corlettk
Succinctly: No, There isn't, as far as I know. A Java array is a fixed-size data-structure. The only way to "logically" resize it is create a new array and copy the wanted elements into the new array.
简而言之:不,据我所知,没有。Java 数组是一种固定大小的数据结构。“逻辑上”调整大小的唯一方法是创建一个新数组并将所需元素复制到新数组中。
Instead: You could (possibly) implement a class which wraps an array into a collection and uses a "size" variable to logically reduce the length of the array without actually copying the values. This an approach has limited utility... The only case I can imagine where it's practical is when you're dealing with a huge array, which just can't be copied because of memory limitations.
相反:您可以(可能)实现一个将数组包装到集合中的类,并使用“大小”变量在逻辑上减少数组的长度,而无需实际复制值。这种方法的实用性有限……我能想象的唯一可行的情况是当您处理一个巨大的数组时,由于内存限制而无法复制它。
Copying an array is relatively inexpensive time-wise... unless you're doing it millions of times, in which case you probably need to think up an algorithm to avoid this, and not waste your time mucking around with a "variable length array".
复制数组在时间上相对便宜......除非你做了数百万次,在这种情况下你可能需要想出一个算法来避免这种情况,而不是浪费你的时间来处理“可变长度数组” ”。
And of course... you could just use an ArrayList instead. Yes?
当然......你可以只使用一个 ArrayList 代替。是的?
回答by Rob
I was thinking about it some more... and just for kicks, how about something like the below.
我正在考虑更多……只是为了踢球,像下面这样的事情怎么样。
Note:This is just a "can it be done?" intellectual exercise in Java hacking. Anybody who attempts to actually use this idea in production code will deserve all the pain that will undoubtedly follow.
注意:这只是一个“可以做到吗?” Java hacking 中的智力练习。任何试图在生产代码中实际使用这个想法的人都应该承受随之而来的所有痛苦。
public class Foo
{
private static byte[] array = new byte[10];
public static void main(String[] arg) throws Exception
{
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
Unsafe unsafe = (Unsafe) field.get(null);
Field arrayField = Foo.class.getDeclaredField("array");
long ptr = unsafe.staticFieldOffset(arrayField);
// doesn't work... there's gotta be a way though!
unsafe.reallocateMemory(ptr, 5);
System.out.println("New array size is: " + array.length);
}
}