<?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; Define Array in java</title>
	<atom:link href="http://www.bestjavainterviewquestions.com/category/java-interview-questions/define-array-in-java/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>Define Array in Java</title>
		<link>http://www.bestjavainterviewquestions.com/define-array-in-java/</link>
		<comments>http://www.bestjavainterviewquestions.com/define-array-in-java/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 17:25:24 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Define Array in java]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=481</guid>
		<description><![CDATA[An Array is a group of similar data type items that share a common name. Arrays occupies contiguous memory locations. Array size is fixed and cannot be altered dynamically. For example, we can define an array name as salary to represent a set of salaries of a group of employees. A particular value is indicated [...]]]></description>
			<content:encoded><![CDATA[<p>An Array is a group of similar data type items that share a common name. Arrays occupies contiguous memory locations. Array size is fixed and cannot be altered dynamically. For example, we can define an array name as <strong>salary</strong> to represent a set of salaries of a group of employees. A particular value is indicated by writing a number called index number or subscript in brackets after the array name.</p>
<p><em> For example</em>:</p>
<p><em><strong>salary[3]</strong></em></p>
<p style="margin-bottom: 0in;">Represents the salary of 4<sup>th</sup> employee(array index starts from zero ). While the complete set of values are referred to as an array, the individual values are called elements. Arrays can be of any primitive data type.</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;"><strong>One-dimensional Arrays :</strong></p>
<p style="margin-bottom: 0in;"><strong><br />
</strong></p>
<p style="margin-bottom: 0in;">A list of item can be given one variable name using only one subscript and such a variable is called as one-dimensional array.</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;"><strong>Creating an Array :</strong></p>
<p style="margin-bottom: 0in;"><strong><br />
</strong></p>
<p style="margin-bottom: 0in;">Arrays must be declared and created in the computer memory before they are used. Creation of an array involves three steps;</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">(i)    Declaring the array</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">(ii)   Creating memory locations</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">(iii)  Putting values in the memory locations</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;"><strong>Declaration of Arrays :</strong></p>
<p style="margin-bottom: 0in;"><strong><br />
</strong></p>
<p style="margin-bottom: 0in;">Arrays in java may be declared in two forms :</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;"><!-- 		@page { size: 8.5in 11in; margin: 0.79in } 		P { margin-bottom: 0.08in } --></p>
<p style="margin-bottom: 0in;"><em>1<sup>st</sup> way : datatype arrayname[];</em></p>
<p style="margin-bottom: 0in;"><em><br />
</em></p>
<p style="margin-bottom: 0in;"><em>2<sup>nd</sup> way : datatype[] arrayname;</em></p>
<p style="margin-bottom: 0in;"><em><br />
</em></p>
<p style="margin-bottom: 0in;"><strong>Examples :</strong></p>
<p style="margin-bottom: 0in;"><strong><br />
</strong></p>
<p style="margin-bottom: 0in;"><em>int number[];</em></p>
<p style="margin-bottom: 0in;"><em>float average[];</em></p>
<p style="margin-bottom: 0in;"><em>int[] counter;</em></p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;"><em>float[] marks;</em></p>
<p style="margin-bottom: 0in;"><em><br />
</em></p>
<p style="margin-bottom: 0in;"><strong>Note: </strong><span>The size of the array should not be mentioned while declaration</span></p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;"><span><br />
</span></p>
<p style="margin-bottom: 0in;"><strong>Creation of Arrays :</strong></p>
<p style="margin-bottom: 0in;"><strong><br />
</strong></p>
<p style="margin-bottom: 0in;">After declaring an array, memory should be allocated. Java allows the creation of arrays using <strong>new </strong>keyword, as shown below:</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;"><em>type arrayname=new type[size];</em></p>
<p style="margin-bottom: 0in;"><em><br />
</em></p>
<p style="margin-bottom: 0in;"><strong>Examples:</strong></p>
<p style="margin-bottom: 0in;"><strong><br />
</strong></p>
<p style="margin-bottom: 0in;"><em>int number[];</em></p>
<p style="margin-bottom: 0in;"><em>number=new int[5];</em></p>
<p style="margin-bottom: 0in;"><em>float average[];</em></p>
<p style="margin-bottom: 0in;"><em>average=new float[10];</em></p>
<p style="margin-bottom: 0in;"><em><br />
</em></p>
<p style="margin-bottom: 0in;">This lines create necessary memory locations for the arrays number and average and designate them as int and float respectively. Now, the variable number refers to an array of 5 integers and average refers to an array of 10 floating point values.</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">It is also possible to combine both the steps&#8211; declaration and creation into aone as shown below.</p>
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;">
<p style="margin-bottom: 0in;"><em>float average  =    new float[10];</em></p>
<p style="margin-bottom: 0in;"><em>int num []       =    new int[5];</em></p>
<p style="margin-bottom: 0in;"><em><br />
</em></p>
<p style="margin-bottom: 0in;"><strong><em>Sample example of array&#8217;s in java are as follows;</em></strong></p>
<p style="margin-bottom: 0in;"><strong><em><br />
</em></strong></p>
<p style="margin-bottom: 0in;"><em>//java program for Total marks and percentage.</em></p>
<p style="margin-bottom: 0in;"><em><br />
</em></p>
<p style="margin-bottom: 0in;"><em>import java.io.*;<br />
class Arr<br />
{<br />
public static void main(String args[])throws IOException<br />
{<br />
//to accept data from keyboard</em></p>
<p style="margin-bottom: 0in;"><em>BufferedReader br=new BufferedReader(new InputStreamReader(System.in));<br />
System.out.println(&#8220;How many Subjects&#8221;);<br />
int n=Integer.parseInt(br.readLine());</em></p>
<p style="margin-bottom: 0in;"><em><br />
</em></p>
<p><em>//create the array with size n<br />
int marks[] = new int[n];</em></p>
<p><em>//store marks into an array marks[]<br />
for(i=0;i&lt;n;i++)<br />
{<br />
System.out.println(&#8220;enter marks&#8221;);<br />
marks[i]=Integer.parseInt(br.readLine());<br />
}</em></p>
<p><em>//display the marks again and final total<br />
int tot=0;<br />
for(int i=0;i&lt;n;i++)<br />
{<br />
System.out.println(marks[i]);<br />
tot=tot+marks[i];<br />
}<br />
System.out.println(&#8220;total marks=&#8221;+tot);</em></p>
<p><em>//final percentage of marks</em></p>
<p><em>float percent=(float)total/n;</em></p>
<p><em>System.out.println(&#8220;percentage=&#8221;+percent);<br />
System.out.println(&#8220;arr size=&#8221;mark.length);<br />
}<br />
}<br />
</em></p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>October 4, 2008 -- <a href="http://www.bestjavainterviewquestions.com/difference-between-executeupdate-and-executequery-methods/" title="Difference between executeUpdate and executeQuery Methods">Difference between executeUpdate and executeQuery Methods</a> (0)</li><li>September 30, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-precompile-of-jsp-page/" title="Define Precompile of Jsp page">Define Precompile of Jsp page</a> (0)</li><li>October 5, 2008 -- <a href="http://www.bestjavainterviewquestions.com/thread-safety-of-the-servlet/" title="Thread safety of the servlet">Thread safety of the servlet</a> (0)</li><li>November 2, 2008 -- <a href="http://www.bestjavainterviewquestions.com/arraylist-class/" title="ArrayList Class">ArrayList Class</a> (2)</li><li>September 8, 2008 -- <a href="http://www.bestjavainterviewquestions.com/servlet-session-examples/" title="Servlets Session Examples">Servlets Session Examples</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/define-array-in-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

