ListToArray.java
ArrayList<String> arrayList = new ArrayList<String>();
for(int i=0; i < 10000000;i++) { //← 100 million
arrayList.add("A");
}
long array1Start = System.currentTimeMillis();
arrayList.toArray(new String[arrayList.size()]); //←← here
long array1End = System.currentTimeMillis();
System.out.println("processing time:" + (array1End - array1Start) + " ms");
Processing time: 3179 ms
ListToArray.java
ArrayList<String> arrayList = new ArrayList<String>();
for(int i=0; i < 100000000;i++) { //← 100 million
arrayList.add("A");
}
long array2Start = System.currentTimeMillis();
arrayList.toArray(new String[0]); //←← here
long array2End = System.currentTimeMillis();
System.out.println("processing time:" + (array2End - array2Start) + " ms");
Processing time: 3047 ms
Eh, almost the same ... Depending on the situation of the machine at that time, the result will be almost the same.
Processing time is almost the same Is it because it just copies the reference source?
arrayList.toArray(new String[arrayList.size()]); //Explicit and easy to understand
arrayList.toArray(new String[0]); //Think about the meaning of 0
Performance is almost the same, Size should be specified for readability
Recommended Posts