阅读文本文件还是向ArrayList添加元素更快?

huangapple go评论97阅读模式
英文:

Is faster to read text file or add elements to ArrayList?

问题

我目前是通过子方法将元素添加到ArrayList中,然后在主函数中调用它:

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

问题是我会有200个不同的产品代码。
所以我的问题是,添加元素到ArrayList的最快方法是什么?像我现在这样做,还是从文本文档中读取?对于这个问题有更好的方法吗?

提前谢谢。

英文:

I currently add elements to ArrayList using submethod and then call it in the main 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.
So my question is what is the fastest way to add elements to the 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.

答案1

得分: 2

你可以自行测试这个。只需执行:

long start = System.nanoTime();
// 手动添加
System.out.println("花费时间:" + (System.nanoTime() - start));

以及

long start = System.nanoTime();
// 从文件中读取
System.out.println("花费时间:" + (System.nanoTime() - start));
英文:

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));

答案2

得分: 1

绝对会更慢的是 I/O 操作。您需要提供更多细节,例如这些代码更改频率如何?我怀疑您并不是要将它们手动直接添加到代码中。

英文:

Definitely an I/O operation would be slower. 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.

huangapple
  • 本文由 发表于 2020年4月5日 02:00:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/61032443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定