简体   繁体   English

List和Enumeration之间的区别

[英]Difference between the List and Enumeration

I have a written a method to get all the records and return in the List Type, but I got out of memory error. 我有一个写一个方法来获取所有记录并返回列表类型,但我失去了内存错误。 So I changed return type from List to Enumeration, in the method, instead of ArrayList, uses Vector and return vector.elements at the end of the method. 所以我将返回类型从List更改为Enumeration,在方法中,而不是ArrayList,使用Vector并在方法的末尾返回vector.elements。 It works without any error. 它没有任何错误。 but I did not understand why. 但我不明白为什么。

Could someone explain Why this Enumeration worked? 有人可以解释为什么这个枚举有效吗?

Enumeration is the "old version" of Iterator . EnumerationIterator的“旧版本”。
Vector is the "old version" of ArrayList . VectorArrayList的“旧版本”。

The memory difference is not supposed to be significant, so perhaps the fluctuations you've observed are due to another thing. 记忆差异不应该是显着的,所以你观察到的波动可能是另一回事。

Depending on the size of the list you may need to increase the maximum memory of the JVM (using Xmx , Xms and/or XX:MaxPermSize ) 根据列表的大小,您可能需要增加JVM的最大内存(使用XmxXms和/或XX:MaxPermSize

You must have fixed something else. 你必须修复别的东西。 A Vector will use if anything more memory than an ArrayList, and returning an Enumeration instead of the list itself only adds a tiny bit more memory usage, unless your caller was using a list iterator in which case it is line ball. Vector将使用比ArrayList 更多的内存,并且返回枚举而不是列表本身只会增加更多的内存使用量,除非你的调用者使用列表迭代器,在这种情况下它是行球。 There's certainly no reason for this strategy to use significantly less memory. 这种策略肯定没有理由使用明显更少的内存。

Unless you were returning a copy of the original list? 除非您返回原始列表的副本 as a new ArrayList? 作为一个新的ArrayList? That would double the memory usage at least while the copy was being made, but it would have to be a very long list ... 这至少会在制作副本时将内存使用量增加一倍,但它必须是一个非常长的列表......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM