在O(1)时间初始化Java的阵列

发布于 2025-01-30 01:30:43 字数 370 浏览 2 评论 0 原文

是否可以在O(1)时间中初始化Java中的数组。在C \ C ++中,这是可能的,因为该语言不会自动将数组初始化为0。据我所知,在Java中,没有办法跳过自动初始化步骤。

为了澄清,我的意思是使用具有所有数组功能的专业数据结构,并增加了将所有数组元素初始化为恒定时间(例如o(1)复杂性)的能力(例如零)。这通常是使用3个非初始化阵列和一个计数器完成的。

有关此方法的更多信息,

Is it possible to initialize an array in java in O(1) time. In C\C++ it is possible because the language doesn't automatically initialize the array to 0. In Java, as far as I know, there's no way to skip the automatic initializing step.

To clarify, what I mean is using a specialized data structure that has all array functionality with the addition of the capability of initializing all array elements to a certain value (like zero) in constant time (in O(1) complexity). This is usually done using 3 uninitialized arrays and a counter.

For More info about this method https://eli.thegreenplace.net/2008/08/23/initializing-an-array-in-constant-time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

糖粟与秋泊 2025-02-06 01:30:43

在Java中,您不能分配一个非初始化的数组。

您可以使用Unsafe.AlocateMory方法分配非初始化的内存,该方法将返回指针,但这是一个私有API ,它可能在任何时候都无法使用,并且已经部分不可用 jep 403

In Java you cannot allocate an uninitialized array.

You can allocate uninitialized memory with the Unsafe.allocateMemory method, which will return a pointer, but this is a private API that may become unavailable at any time and it is already partially unavailable JEP 403.

久夏青 2025-02-06 01:30:43

boolean),0(int)等初始化数组

t [] arr = new t [n]; 将使用默认值:null(object,false ( [] arr = {a,b,c}; 或 t [] arr = new t [] {a,b,c};
可以更好地实施哪个

现在考虑:

int[] arr = new int[100_000];
Arrays.fill(arr, 13);

新数组的Jave字节代码指令 aneWarray 通常会归因许多元素,尽管 arrays.fill.fill 。尽管JIT或AOT编译器可能会在实际机器说明中优化组合代码。

速度损失是相对的,因为执行仍然很快,并且通常应进一步处理数组元素。

对于稀疏数组,还有其他数据结构,例如 map< integer,Integer> 。布尔数组可以使用更紧凑,更快的 bitset

在C中,数组分配不一定为元素为零:数据可能是统一的,或者 - 甚至更糟的是回收内存。因此,Java 初始化数组:更好的代码质量。

Java中有一些不安全的内存功能,但是使用它们是毫无意义的。但是,您可以在最新的Java版本中使用非Java-Heap内存。
通过Java的计算,这甚至通常会更慢。
因此,最好在非常关键的情况下使用C。

T[] arr = new T[n]; will initialize the array with the defaults: null (Object, false (boolean), 0 (int) and so on.

You could do T[] arr = {a, b, c}; or T[] arr = new T[]{a, b, c};.
Which might have been implemented better - but is not.

Now consider:

int[] arr = new int[100_000];
Arrays.fill(arr, 13);

The jave byte code instruction for a new array anewarray will normally zero the many elements, despite being overwritten by Arrays.fill. Though a JIT or AOT compiler might optimize the combined code in real machine instructions.

The speed loss is relative, as the execution is still fast, and normally array elements should be processed further on.

For sparse arrays there are other data structures, like Map<Integer, Integer>. Boolean arrays can use the more compact and faster BitSet.

In C an array allocation does not necessarily zero the elements: the data may be unitialized or - even worse - reclaimed memory. For that reason java initializes arrays: better code quality.

There are some unsafe memory features in java, but it would be senseless to use them. However you can use non-java-heap memory in the newest java version.
With calculations in java, that would even be more slow normally.
So better use C in very critical cases.

爱人如己 2025-02-06 01:30:43

在Java,C和C ++中,您可以创建一个数据结构,该数据结构赋予数组功能,而无需为元素分配内存,直到实际需要为止。这有时是针对非常大的阵列进行的,其中大多数元素都不会使用。稀疏阵列和稀疏矩阵就是示例。它以增加访问元素所需的时间为代价减少了初始化时间。我将考虑本文其余部分的稀疏阵列。

假设 sparsenode 具有 int index double value 以及可能的其他实例变量。 索引指示如果它在正常数组中的位置。 sparsearray 类可能包括以下方法: public void setValue(int index,double value) public double double double getValue(int index)。我会忽略抛出例外的可能性。

如果 sparsearray linkedlist 支持,则使用这些方法中的任何一种都可能涉及对元素的顺序搜索,直到找到具有匹配索引的元素或确定它元素不在数组中。

如果找不到元素,则 getValue 方法将返回默认值,对于大多数稀疏数组应用程序而言,这将是零。 set 方法将将元素添加到列表中。

在链接列表中,您可以指定新元素的位置,以使其保持秩序。如果数组元素不在列表中,则搜索将终止,尽管搜索仍然是O(n)时间复杂性。

有多种方法可以通过使用连续搜索 getValue setValue 来提高性能。如果将元素按顺序保存,则有可能使用二进制搜索。另一种可能性是使用搜索树,或者作为另一个答案,使用哈希函数的结构。

In Java, C, and C++, you could create a data structure that gives the functionality of an array, without allocating memory for an element until it is actually needed. This is sometimes done for very large arrays in which most of the elements will not be used. A sparse array and sparse matrix are examples. It reduces the initialization time at the cost of increasing the time needed to access elements. I'll consider a sparse array for the rest of this post.

Suppose a SparseNode has int index, double value, and possibly other instance variables. The index indicates where it would be if it were in a normal array. The SparseArray class might include these methods: public void setValue (int index, double value) and public double getValue (int index). I'll ignore the possibility of throwing exceptions.

If the SparseArray is backed by a LinkedList, use of either of those methods could involve a sequential search of the elements until an element with matching index is found or it is determined the element is not in the array.

If the element is not found, the getValue method would return the default value, which would be zero for most sparse array applications. The set method would add the element to the list.

In a linked list, you could specify a location of a new element in order to keep them in order. If an array element is not in the list, the search would terminate earlier, although the search would still be O(n) time complexity.

There are ways to improve performance over using a sequential search for every call to getValue or setValue. If the elements are kept in order, there is the possibility of using a binary search. Another possibility is the use of a search tree, or as another answer suggested, a structure using hash functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文