[Java] ArrayList → Should I specify the size in array conversion?

When specifying the size of the array

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

result

Processing time: 3179 ms

If you do not specify the size of the array

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

result

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.

evaluate

performance

Processing time is almost the same Is it because it just copies the reference source?

readability

arrayList.toArray(new String[arrayList.size()]); //Explicit and easy to understand
arrayList.toArray(new String[0]);                //Think about the meaning of 0

Conclusion

Performance is almost the same, Size should be specified for readability

Recommended Posts

[Java] ArrayList → Should I specify the size in array conversion?
Specify the java location in eclipse.ini
I tried the new era in Java
Organized memo in the head (Java --Array)
Measure the size of a folder in Java
I tried to implement the Euclidean algorithm in Java
[Java] List type / Array type conversion
I translated [Clone method for Java arrays] as the Clone method in Java arrays.
I made roulette in Java.
Java beginners briefly summarized the behavior of Array and ArrayList
Regarding the difference between Java array and ArrayList, I compared and corresponded methods with similar functions.
I was confused because there was a split in the Array
[Java] I implemented the combination.
[Java] Convert ArrayList to array
I studied the constructor (java)
I tried metaprogramming in Java
I want to simplify the conditional if-else statement in Java
Find out the maximum heap size when you specify the -Xmx or -XX: MaxRAM option in Java
I made the "Sunshine Ikezaki game" I saw on Twitter in Java.
[Java] I want to perform distinct with the key in the object
Access the network interface in Java
Guess the character code in Java
I created a PDF in Java.
I read the source of ArrayList I read
Unzip the zip file in Java
Java basic learning content 2 (array / ArrayList)
I wrote Goldbach's theorem in java
I tried the Java framework "Quarkus"
Parsing the COTOHA API in Java
[Java] Difference between array and ArrayList
I made an annotation in Java.
I tried using JWT in Java
[Java] Conversion from array to List
Java array / list / stream mutual conversion list
[Java] Convert array to ArrayList * Caution
Call the super method in Java
I tried to solve the past 10 questions that should be solved after registering with AtCoder in Java
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
Specify the order in which configuration files and classes are loaded in Java
I called the COTOHA API parser 100 times in Java to measure performance.
I want to get the IP address when connecting to Wi-Fi in Java
I want to ForEach an array with a Lambda expression in Java
[Java] I participated in ABC-188 of Atcorder.
I tried using Elasticsearch API in Java
Specify the default JAVA_HOME used in buildship
When seeking multiple in a Java array
Get the result of POST in Java
Java reference to understand in the figure
Try using the Stream API in Java
Call the Windows Notification API in Java
Map without using an array in java
How to do base conversion in Java
[Java] Use cryptography in the standard library
Compare the elements of an array (Java)
Try calling the CORBA service in Java 11+
[day: 5] I summarized the basics of Java
What is the main method in Java?
Easily measure the size of Java Objects
[Java] I personally summarized the basic grammar.
Immutable (immutable) List object conversion function in Java8
How to get the date in java