<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Core9x</title>
    <description>The latest articles on DEV Community by Core9x (@thanhtungdlvn).</description>
    <link>https://dev.to/thanhtungdlvn</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1266676%2F9e213750-6877-4f40-84ec-9ff985a8cd4b.png</url>
      <title>DEV Community: Core9x</title>
      <link>https://dev.to/thanhtungdlvn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thanhtungdlvn"/>
    <language>en</language>
    <item>
      <title>ArrayList in Java</title>
      <dc:creator>Core9x</dc:creator>
      <pubDate>Sun, 20 Apr 2025 05:18:20 +0000</pubDate>
      <link>https://dev.to/thanhtungdlvn/arraylist-in-java-5dm5</link>
      <guid>https://dev.to/thanhtungdlvn/arraylist-in-java-5dm5</guid>
      <description>&lt;p&gt;&lt;em&gt;All elements are stored in contiguous memory locations, like a traditional array, but with the ability to resize dynamically.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GET&lt;/strong&gt; Operation:&lt;/p&gt;

&lt;p&gt;Getting an element at a specific index takes O(1) time complexity.&lt;/p&gt;

&lt;p&gt;The formula for retrieving an element at a specific index:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;memory_address = base_address + (index * element_size)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;base_address: the address of the first element element_size: 4 bytes for int, 8 bytes for double,…&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Index&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;th&gt;Memory Address&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;1000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;1004&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;td&gt;1008&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;40&lt;/td&gt;
&lt;td&gt;1012&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;base_address = 1000 (address of the first element)&lt;/li&gt;
&lt;li&gt;element_size = 4 bytes (for int)&lt;/li&gt;
&lt;li&gt;To access the value at index 2: 1000 + (2 *4) = 1008&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;INSERT&lt;/strong&gt; Operation:&lt;/p&gt;

&lt;p&gt;Requires shifting subsequent elements (especially when inserting at the middle or beginning)&lt;/p&gt;

&lt;p&gt;The complexity is O(n)&lt;/p&gt;

&lt;p&gt;Best case: Adding an element to the end of the list takes O(1) time complexity when there is enough capacity.&lt;/p&gt;

&lt;p&gt;Worst case: Adding an element to the middle or beginning of the list takes O(n) time complexity&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DELETE&lt;/strong&gt; Operation:&lt;/p&gt;

&lt;p&gt;Requires shifting subsequent elements (especially when inserting at the middle or beginning)&lt;/p&gt;

&lt;p&gt;The complexity is O(n)&lt;/p&gt;

&lt;p&gt;Best case: Deleting the last element of the list has a time complexity of O(1) since no shifting is required&lt;/p&gt;

&lt;p&gt;Worst case: Deleting an element at the middle or beginning of the list has a time complexity of O(n), which requires shifting all subsequent elements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RESIZING&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;The default capacity size is 10&lt;/p&gt;

&lt;p&gt;When the ArrayList exceeds its current capacity, it will create a new, 50% larger array and copy all existing elements into a new array.&lt;/p&gt;

&lt;p&gt;Example of Resizing:&lt;/p&gt;

&lt;p&gt;Initial capacity: 10.&lt;br&gt;
When adding the 11th element:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A new array of size 15 (10 + 50% of 10) is created.&lt;/li&gt;
&lt;li&gt;The existing 10 elements are copied into the new array.&lt;/li&gt;
&lt;li&gt;The new element is then added.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;SUMMARY&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;An ArrayList is not synchronized and stores its elements in contiguous memory locations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;GET operation has O(1) time complexity&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ADDING and REMOVING have O(n) time complexity&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Random access is required&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Read heavy operations&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>arraylist</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
