<?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: Hari Krishna E</title>
    <description>The latest articles on DEV Community by Hari Krishna E (@harikrishna1288).</description>
    <link>https://dev.to/harikrishna1288</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%2F822481%2F1532e23c-a815-4dfa-a491-6e56d37d12ef.png</url>
      <title>DEV Community: Hari Krishna E</title>
      <link>https://dev.to/harikrishna1288</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/harikrishna1288"/>
    <language>en</language>
    <item>
      <title>Java Collections</title>
      <dc:creator>Hari Krishna E</dc:creator>
      <pubDate>Sun, 27 Feb 2022 18:27:38 +0000</pubDate>
      <link>https://dev.to/harikrishna1288/java-collections-2o2n</link>
      <guid>https://dev.to/harikrishna1288/java-collections-2o2n</guid>
      <description>&lt;p&gt;ArrayList is Collection in java.util package which extends AbstractList class and implements List Interface.&lt;br&gt;
It also implements RandomAccess,Cloneable and Serializable interfaces&lt;br&gt;
RandomAccess ,Cloneable and Serializable interfaces doesnot have any methods to implement.&lt;br&gt;
They are to say that ArrayList can be accessed random with constant time and we can use the clone() method and it can be Serializable&lt;/p&gt;

&lt;p&gt;When we add new element for the first time it will have the initial capacity of 10 and then the element will be added.&lt;br&gt;
ArrayList has grow() method which will be called when we start adding more element Arrays.copyOf(elementData,newCapacity) will be returned with the updated Array.&lt;br&gt;
&lt;u&gt;Method in ArrayList :&lt;/u&gt;&lt;br&gt;
&lt;code&gt;&lt;br&gt;
 private void grow(int minCapacity) {&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It checks the range so in case the index that we pass exceeds the size it throws IndexOutOfBoundException.&lt;br&gt;
&lt;u&gt;Method in ArrayList&lt;/u&gt;&lt;br&gt;
&lt;code&gt;&lt;br&gt;
 private void rangeCheck(int index) {&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It has clear method which will loop through each index and set the value to null and let GC take care of it and set the size to 0.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Method in ArrayList&lt;/u&gt;&lt;br&gt;
&lt;code&gt;&lt;br&gt;
public void clear() {&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;writeObject and readObject methods help for Serialization and Deserialization.&lt;br&gt;
&lt;u&gt;Methods in ArrayList&lt;/u&gt;&lt;br&gt;
`&lt;br&gt;
private void writeObject(java.io.ObjectOutputStream s){&lt;/p&gt;

&lt;p&gt;private void readObject(java.io.ObjectInputStream s)&lt;br&gt;
        throws java.io.IOException, ClassNotFoundException {&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example to add elements in ArrayList and iterate in multiple ways&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.corejava.oops.collections;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ArrayListTest {

    public static void main(String[] args) {


        List&amp;lt;String&amp;gt;  test = new ArrayList&amp;lt;&amp;gt;();

        test.add("Tester1"); //index - 0
        test.add("Tester2"); //index - 1
        test.add("Tester3"); //index - 2
        test.add("Tester4"); //index - 3


        for(int i=0;i&amp;lt;test.size();i++) {

            System.out.println("In For Loop :" +test.get(i));

        }

        for(String a : test) {

            System.out.println("In Enhanced for loop : "+a);

        }

        Iterator&amp;lt;String&amp;gt; it = test.iterator();

        while(it.hasNext()) {
            System.out.println("In While Loop :" + it.next());

        }

        test.stream().forEach(System.out::println);

    }

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javacollections</category>
      <category>arraylist</category>
    </item>
  </channel>
</rss>
