简体   繁体   中英

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:

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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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