首页 \ 问答 \ list.clear()vs list = new ArrayList ();(list.clear() vs list = new ArrayList(); [duplicate])

list.clear()vs list = new ArrayList ();(list.clear() vs list = new ArrayList(); [duplicate])

这个问题已经在这里有一个答案:

  • 清空一个ArrayList或者只是创建一个新的,让旧的被垃圾收集? [复制] 4个答案
  • Map.clear()vs new Map:哪一个会更好? [复制] 7个答案

清除ArrayList的2个选项中的哪一个更好,更快,为什么?

list.clear() 

要么

list = new ArrayList<Integer>();

事情发生在随机的时候,我必须清除我的ArrayList中的所有条目,并且我无法知道将来会有多少个新条目,可能有0或1000.哪种方法更快更好,为什么?


Which one of the 2 options is better and faster to clear an ArrayList, and why?

list.clear() 

or

list = new ArrayList<Integer>();

It happens that I have to, at random times, clear all entries from my ArrayList and I have no way to know how many new entries there will be in the future, there might be 0 or a 1000. Which method is faster and better, and why?


原文:https://stackoverflow.com/questions/6961356
更新时间:2022-03-29 09:03

最满意答案

逐点:

  • 快速解析:二进制(因为你不一定要“解析”,你可以反序列化)
  • 添加新事物:文字
  • 较小:文本(即使gzip文本大于二进制文件,也不会太大)。
  • 可读性:文本

所以这是三个文本的投票,二进制的一个点。 就个人而言,除了图片(以及其他“自然”二进制数据)以外,我都会使用文本。 然后,将所有内容存储在一个大的zip文件中(我可以想到几个游戏做这个或者接近它的东西)。

好读: 纯文本的原文力量 的重要性


Point by point:

  • Fast Parsing: binary (since you don't necessarily have to "parse", you can just deserialize)
  • Adding New Things: text
  • Smaller: text (even if gzipped text is larger than binary, it won't be much larger).
  • Readability: text

So that's three votes for text, one point for binary. Personally, I'd go with text for everything except images (and other data which is "naturally" binary). Then, store everything in a big zip file (I can think of several games do this or something close to it).

Good reads: The Importance of Being Textual and Power Of Plain Text.

相关问答

更多
  • 我使用boost.serialization来存储表示查找表的矩阵和向量,以及一些内存大小约为200MB的元数据(字符串)。 IIRC用于从磁盘加载到内存中,文本存档需要3分钟,而使用WinXP上的二进制存档需要4秒。 I used boost.serialization to store matrices and vectors representing lookup tables and some meta data (strings) with an in memory size of about 2 ...
  • 您可以使用此问题的答案中提到的选项SORT_PROPERTIES_ALPHABETICALLY来实现稳定的序列化(以及您已经提到的选项ORDER_MAP_ENTRIES_BY_KEYS )。 这些电子邮件中解释了更多背景知识: “......在没有显式声明(通过@JsonPropertyOrder)和一般机制(按字母顺序排序; Object Id和Type Id在其他属性之前)之前,没有定义的排序。” 和 “有一点需要注意的是,Oracle确实改变了JDK 7的行为,因此以前对Introspection返回 ...
  • 逐点: 快速解析:二进制(因为你不一定要“解析”,你可以反序列化) 添加新事物:文字 较小:文本(即使gzip文本大于二进制文件,也不会太大)。 可读性:文本 所以这是三个文本的投票,二进制的一个点。 就个人而言,除了图片(以及其他“自然”二进制数据)以外,我都会使用文本。 然后,将所有内容存储在一个大的zip文件中(我可以想到几个游戏做这个或者接近它的东西)。 好读: 纯文本的原文和力量 的重要性 。 Point by point: Fast Parsing: binary (since you don' ...
  • 1)使用Assembly assembly = Assembly.LoadFrom("dllPath"); 而不是Assembly.LoadFile() 2)由于您的插件具有不同的名称,您可以将它们放在一个目录中,并在app.config中为此目录定义路径。 3)在主AppDomain以外加载Dll可能是个好主意,这样你就可以卸载它们而无需重新启动应用程序,请参阅我的示例 1) Use Assembly assembly = Assembly.LoadFrom("dllPath"); instead of ...
  • 好吧,显然有两个独立的东西:DOM作为文档对象模型,DOM作为DOM解析器。 我只是希望他们的名字不同。 ;-) OK, apparently there are 2 separate thing: DOM as a Document Object Model and DOM as a DOM parser. I just wish they would be named differently. ;-)
  • formatter功能运行时没有momentjs上下文。 我猜是因为它消耗formatter属性的方式。 在这里找到的代码中,代码是: if (this.code.formatter) { ... (ctx, varName, this.options.formatter) 由于this关键字有趣的方式,它绑定到对象( options ),并且由于该声明不包含momentjs ,它表示它没有被定义。 通过查看第735行,您可以更好地了解这一点: ctx.pushCode("{0} = ({1}). ...
  • 那么,有一件事.data()没有终端\ 0。 这不是一个C字符串。 我甚至没有意识到stringstream有一个char *构造函数(谁在他们正确的心态使用它们了?),但显然它确实,我敢打赌它期望\ 0。 无论如何,你为什么试图这样做? 用C ++字符串工作会更好。 初始化是用os.str()。 编辑:二进制数据包含大量的\ 0字符和std :: string(char *)构造函数停在第一个。 您的反序列化例程将不可避免地尝试读取流尾(因为它不完整)。 将buf传递到stringstream时,使用st ...
  • 在您可以反序列化或读取流之前,您必须先回到流的开头示例:ms.Seek(0,SeekOrigin.Begin); static void Main(string[] args) { testclass test = new testclass(); IFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(new byte[512],0,512,true,true ...
  • 谷歌协议缓冲区和MessagePack是最有效的方案,不幸的是目前Delphi的端口很少。 Google Protocol Buffers http://sourceforge.net/projects/protobuf-delphi/ 如果你愿意为自己实现(messagepack非常简单),我建议你破解像SuperObject这样的现有库的write()和parse()函数。 你最终会得到一个非常体面的图书馆而不需要太多努力。 Google Protocol Buffers and MessagePack ...
  • Python字符串可以包含二进制数据,因此SerializeAsString返回二进制数据。 Python strings can hold binary data, therefore SerializeAsString returns binary data.

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)