<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java Interview Questions &#187; ArrayList Class</title>
	<atom:link href="http://www.bestjavainterviewquestions.com/category/java-util-questions/arraylist-class/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bestjavainterviewquestions.com</link>
	<description>Java Interview Questions &#124; Core Java Interview Questions &#124; Advanced Java Interview Questions &#124; EJB Interview Questions &#124; J2EE Interview Questions &#124; Hibernate Interview Questions</description>
	<lastBuildDate>Fri, 03 Feb 2012 10:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ArrayList Class</title>
		<link>http://www.bestjavainterviewquestions.com/arraylist-class/</link>
		<comments>http://www.bestjavainterviewquestions.com/arraylist-class/#comments</comments>
		<pubDate>Sun, 02 Nov 2008 16:38:17 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[ArrayList Class]]></category>
		<category><![CDATA[arraylist]]></category>
		<category><![CDATA[define arraylist]]></category>
		<category><![CDATA[List]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=426</guid>
		<description><![CDATA[The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed. In Java, standard arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you may not know util run time precisely how large of an array you need. [...]]]></description>
			<content:encoded><![CDATA[<p>The <strong>ArrayList</strong> class extends <strong>AbstractList</strong> and implements the <strong>List </strong>interface. <strong>ArrayList</strong> supports dynamic arrays that can grow as needed. In Java, standard arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you may not know util run time precisely how large of an array you need. To handle this situation, the collections framework defines ArrayList.</p>
<p>In essence, an ArrayList is a variable-length array of object references. Thjat is, an <strong>ArrayList </strong>can dynamically increase or decrease in size. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the arraya may be shrunk.</p>
<p>It is dynamically growing array that stores objects. It is not Synchronized and Vector is a Synchronized</p>
<p><strong>Note: </strong>You can&#8217;t store primitive data type. we can store only object into ArrayList.</p>
<p><strong>ArrayList</strong> has the constructors shown here:</p>
<p><strong>&#8211;&gt; </strong>ArrayList();  <em>//This constructor builds an empty array list</em></p>
<p><strong>&#8211;&gt; </strong>ArrayList(Collection c)  <em>// This constructor builds an array list that is initialized with the elements of collection <strong>c</strong>.</em></p>
<p><strong>&#8211;&gt; </strong>ArrayList(int <em>capacity</em>)  <em>//This Contructor builds an array list that has the specified initial capacity.</em></p>
<p><em>1) To Creat  an ArrayList</em></p>
<p>ArrayList arl=new ArrayList();  //creating an object</p>
<p>ArrayList Arl=new ArrayList(20);  //creating an object with the size</p>
<p><em>2) To add objects use add() method</em></p>
<p>arl.add(&#8220;APPLE&#8221;);</p>
<p>arl.add(2,&#8221;APPLE&#8221;);</p>
<p><em>3) To Remove objects use remove().</em></p>
<p>arl.remove(&#8220;APPLE&#8221;);  //remove an string</p>
<p>arl.remove(2);  //removing the string  of specified location</p>
<p><em>4) To Know Number of objects, use size()</em></p>
<p>int n= arl.size()   // It displays the size of the array list.</p>
<p><strong>Example of using an ArrayList.</strong></p>
<p>//Demonstrating ArrayList.</p>
<p>import java.util.*;</p>
<p>class Arraylist Demo  {<br />
public static void main(String args[])  {</p>
<p>ArrayList list=new ArrayList();  //creating an object for ArrayList class<br />
System.out.println(&#8220;Initial size of al:&#8221;+al.size());</p>
<p>//add  elements to the array list<br />
al.add(&#8220;C&#8221;);<br />
al.add(&#8220;B&#8221;);<br />
al.add(&#8220;A&#8221;);<br />
al.add(&#8220;D&#8221;);<br />
al.add(&#8220;E&#8221;);<br />
al.add(&#8220;T&#8221;);<br />
al.add(1,&#8221;A2&#8243;);</p>
<p>System.out.println(&#8220;Size of al after additions: &#8220;+ al.size());</p>
<p>//display the array list<br />
System.out.println(&#8220;Content of al:&#8221;+al);</p>
<p>//Remove elements from the array list<br />
al.remove(&#8220;F&#8221;);<br />
al.remove(2);</p>
<p>System.out.println(&#8220;size of al after deletion:&#8221; +al.size());<br />
System.out.println(&#8220;contents of al:&#8221;+al);<br />
}<br />
}</p>
<h2  class="related_post_title">Related Post</h2><ul class="related_post"><li>June 23, 2008 -- <a href="http://www.bestjavainterviewquestions.com/java-util-package-2/" title="JAVA UTIL PACKAGE">JAVA UTIL PACKAGE</a> (0)</li><li>June 23, 2008 -- <a href="http://www.bestjavainterviewquestions.com/19/" title="Java Basic Questions">Java Basic Questions</a> (1)</li><li>May 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/core-java-interview-questions/" title="Core Java Interview Questions">Core Java Interview Questions</a> (1)</li><li>May 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/basic-java-interview-question/" title="Basic Java Interview Question">Basic Java Interview Question</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/arraylist-class/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

