简体   繁体   English

读取文本文件或向 ArrayList 添加元素是否更快?

[英]Is faster to read text file or add elements to ArrayList?

I currently add elements to ArrayList using submethod and then call it in the main function:我目前使用子方法向 ArrayList 添加元素,然后在主 function 中调用它:

public void addProductAll(){
        productCode.add("202LED");
        productCode.add("202WLED");
        productCode.add("WWR"); 
        productCode.add("CUBLED"); 

The problem is that I will have 200 different product codes.问题是我将有 200 个不同的产品代码。 So my question is what is the fastest way to add elements to the ArrayList?所以我的问题是向 ArrayList 添加元素的最快方法是什么? Like I am doing now or maybe read them from text document?就像我现在正在做的那样,或者也许从文本文档中读取它们? Is there a better approach to this problem?有没有更好的方法来解决这个问题?

Thank you in advance.先感谢您。

You can test this yourself.你可以自己测试一下。 Just do:做就是了:

long start = System.nanoTime();
// Add manually
System.out.println("Time taken: " + (System.nanoTime() - start));

and

long start = System.nanoTime();
// Read from file
System.out.println("Time taken: " + (System.nanoTime() - start));

Definitely an I/O operation would be slower.绝对 I/O 操作会更慢。 You need to provide a few more details, for example how often do those codes change?您需要提供更多详细信息,例如这些代码多久更改一次? I doubt you mean to add them manually directly into the code.我怀疑您的意思是将它们直接手动添加到代码中。

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

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