Note that there are some explanatory texts on larger screens.

plurals
  1. POTime comparison of add method for: ArrayList, LinkedList, ArrayList (initialized with some value)
    text
    copied!<p>I wish to know more about data structure and about their implementations (in JAVA language). Today I wrote a test to compare (time comparison) different implementations of the ADT List. Specifically I compared add method, here's my test:</p> <pre><code>@Test public void testTime() { long i = 10000000; long INITIALIZED_VALUE=5000000; List&lt;Integer&gt; arrayBasedList = new ArrayList&lt;Integer&gt;(); List&lt;Integer&gt; linkedBasedList = new LinkedList&lt;Integer&gt;(); List&lt;Integer&gt; arrayBasedInitialedSizeList = new ArrayList&lt;Integer&gt; (INITIALIZED_VALUE); long t1 = System.currentTimeMillis(); for (int index = 0; index &lt;= i; index++) { arrayBasedList.add(index); } long t1End = System.currentTimeMillis() - t1; long t2 = System.currentTimeMillis(); for (int index = 0; index &lt;= i; index++) { linkedBasedList.add(index); } long t2End = System.currentTimeMillis() - t2; long t3 = System.currentTimeMillis(); for (int index = 0; index &lt;= i; index++) { arrayBasedInitialedSizeList.add(index); } long t3End = System.currentTimeMillis() - t3; System.out.println("ArrayBased: " + t1End); System.out.println("LinkedList:" + t2End); System.out.println("ArrayBasedInitializedSize: " + t3End); System.out.println("End"); } </code></pre> <p>And I obtained this result: </p> <blockquote> <p>ArrayBased: 5681 LinkedList:12830 ArrayBasedInitializedSize: 858</p> </blockquote> <p>Why LinkedList is slower than ArrayList implementation? I thought that add method, for LinkedList implementation, was faster than add method for Array implementation.</p> <p>Anyone can explain me why array is faster than linkedlist for the add method?</p> <p>Thanks</p> <p>Alessio</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload