{"id":1174,"date":"2022-02-24T19:38:32","date_gmt":"2022-02-24T19:38:32","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1174"},"modified":"2022-02-24T19:38:32","modified_gmt":"2022-02-24T19:38:32","slug":"java-arrays","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/24\/java-arrays\/","title":{"rendered":"Java Arrays"},"content":{"rendered":"\n<p>In this tutorial, we will learn to work with arrays in Java. We will learn to declare, initialize, and access array elements with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">An array is a collection of similar types of data.<\/p>\n\n\n\n<p>For example, if we want to store the names of 100 people then we can create an array of the string type that can store 100 names.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String&#91;] array = new String&#91;100];<\/code><\/pre>\n\n\n\n<p>Here, the above array cannot store more than 100 names. The number of values in a Java array is always fixed.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"declaration\">How to declare an array in Java?<\/h2>\n\n\n\n<p>In Java, here is how we can declare an array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dataType&#91;] arrayName;<\/code><\/pre>\n\n\n\n<ul><li><var>dataType<\/var>\u00a0&#8211; it can be\u00a0primitive data types\u00a0like\u00a0<code>int<\/code>,\u00a0<code>char<\/code>,\u00a0<code>double<\/code>,\u00a0<code>byte<\/code>, etc. or\u00a0Java objects<\/li><li><var>arrayName<\/var>\u00a0&#8211; it is an\u00a0identifier<\/li><\/ul>\n\n\n\n<p>For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>double&#91;] data;<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<var>data<\/var>&nbsp;is an array that can hold values of type&nbsp;<code>double<\/code>.<\/p>\n\n\n\n<p><strong>But, how many elements can array this hold?<\/strong><\/p>\n\n\n\n<p>Good question! To define the number of elements that an array can hold, we have to allocate memory for the array in Java. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ declare an array\ndouble&#91;] data;\n\n\/\/ allocate memory\ndata = new double&#91;10];<\/code><\/pre>\n\n\n\n<p>Here, the array can store&nbsp;<strong>10<\/strong>&nbsp;elements. We can also say that the&nbsp;<strong>size or length<\/strong>&nbsp;of the array is 10.<\/p>\n\n\n\n<p>In Java, we can declare and allocate the memory of an array in one single statement. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>double&#91;] data = new double&#91;10];<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"initialization\">How to Initialize Arrays in Java?<\/h2>\n\n\n\n<p>In Java, we can initialize arrays during declaration. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/declare and initialize and array\nint&#91;] age = {12, 4, 5, 2, 5};<\/code><\/pre>\n\n\n\n<p>Here, we have created an array named age and initialized it with the values inside the curly brackets.<\/p>\n\n\n\n<p>Note that we have not provided the size of the array. In this case, the Java compiler automatically specifies the size by counting the number of elements in the array (i.e. 5).<\/p>\n\n\n\n<p>In the Java array, each memory location is associated with a number. The number is known as an array index. We can also initialize arrays in Java, using the index number. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ declare an array\nint&#91;] age = new int&#91;5];\n\n\/\/ initialize array\nage&#91;0] = 12;\nage&#91;1] = 4;\nage&#91;2] = 5;\n..<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/initialize-array-during-declaration-java.jpg\" alt=\"Elements are stored in the array\" title=\"Array initialization\"\/><figcaption>Java Arrays initialization<\/figcaption><\/figure>\n\n\n\n<p><strong>Note<\/strong>:<\/p>\n\n\n\n<ul><li>Array indices always start from 0. That is, the first element of an array is at index 0.<\/li><li>If the size of an array is&nbsp;<var>n<\/var>, then the last element of the array will be at index&nbsp;<var>n-1<\/var>.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"access-array\">How to Access Elements of an Array in Java?<\/h2>\n\n\n\n<p>We can access the element of an array using the index number. Here is the syntax for accessing elements of an array,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ access array elements\narray&#91;index]<\/code><\/pre>\n\n\n\n<p>Let&#8217;s see an example of accessing array elements using index numbers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Access Array Elements<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n public static void main(String&#91;] args) {\n  \n   \/\/ create an array\n   int&#91;] age = {12, 4, 5, 2, 5};\n\n   \/\/ access each array elements\n   System.out.println(\"Accessing Elements of Array:\");\n   System.out.println(\"First Element: \" + age&#91;0]);\n   System.out.println(\"Second Element: \" + age&#91;1]);\n   System.out.println(\"Third Element: \" + age&#91;2]);\n   System.out.println(\"Fourth Element: \" + age&#91;3]);\n   System.out.println(\"Fifth Element: \" + age&#91;4]);\n }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Accessing Elements of Array:\nFirst Element: 12\nSecond Element: 4\nThird Element: 5\nFourth Element: 2\nFifth Element: 5<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, notice that we are using the index number to access each element of the array.<\/p>\n\n\n\n<p>We can use loops to access all the elements of the array at once.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"looping-array\">Looping Through Array Elements<\/h2>\n\n\n\n<p>In Java, we can also loop through each element of the array. For example,<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Using For Loop<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n public static void main(String&#91;] args) {\n  \n   \/\/ create an array\n   int&#91;] age = {12, 4, 5};\n\n   \/\/ loop through the array\n   \/\/ using for loop\n   System.out.println(\"Using for Loop:\");\n   for(int i = 0; i &lt; age.length; i++) {\n     System.out.println(age&#91;i]);\n   }\n }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Using for Loop:\n12\n4\n5<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we are using the\u00a0for Loop in Java\u00a0to iterate through each element of the array. Notice the expression inside the loop,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>age.length<\/code><\/pre>\n\n\n\n<p>Here, we are using the&nbsp;<code>length<\/code>&nbsp;property of the array to get the size of the array.<\/p>\n\n\n\n<p>We can also use the\u00a0for-each loop\u00a0to iterate through the elements of an array. For example,<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Using the for-each Loop<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n public static void main(String&#91;] args) {\n  \n   \/\/ create an array\n   int&#91;] age = {12, 4, 5};\n\n   \/\/ loop through the array\n   \/\/ using for loop\n   System.out.println(\"Using for-each Loop:\");\n   for(int a : age) {\n     System.out.println(a);\n   }\n }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Using for-each Loop:\n12\n4\n5<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example: Compute Sum and Average of Array Elements<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n public static void main(String&#91;] args) {\n\n   int&#91;] numbers = {2, -9, 0, 5, 12, -25, 22, 9, 8, 12};\n   int sum = 0;\n   Double average;\n   \n   \/\/ access all elements using for each loop\n   \/\/ add each element in sum\n   for (int number: numbers) {\n     sum += number;\n   }\n  \n   \/\/ get the total number of elements\n   int arrayLength = numbers.length;\n\n   \/\/ calculate the average\n   \/\/ convert the average from int to double\n   average =  ((double)sum \/ (double)arrayLength);\n\n   System.out.println(\"Sum = \" + sum);\n   System.out.println(\"Average = \" + average);\n }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Sum = 36\nAverage = 3.6<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created an array of named&nbsp;<var>numbers<\/var>. We have used the&nbsp;<code>for...each<\/code>&nbsp;loop to access each element of the array.<\/p>\n\n\n\n<p>Inside the loop, we are calculating the sum of each element. Notice the line,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int arrayLength = number.length;<\/code><\/pre>\n\n\n\n<p>Here, we are using the\u00a0length attribute\u00a0of the array to calculate the size of the array. We then calculate the average using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>average = ((double)sum \/ (double)arrayLength);<\/code><\/pre>\n\n\n\n<p>As you can see, we are converting the&nbsp;<code>int<\/code>&nbsp;value into&nbsp;<code>double<\/code>. This is called type casting in Java. To learn more about typecasting, visit&nbsp;<a href=\"https:\/\/www.programiz.com\/java-programming\/typecasting\">Java Type Casting<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"multidimensional\">Multidimensional Arrays<\/h3>\n\n\n\n<p>Arrays we have mentioned till now are called one-dimensional arrays. However, we can declare multidimensional arrays in Java.<\/p>\n\n\n\n<p>A multidimensional array is an array of arrays. That is, each element of a multidimensional array is an array itself. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>double&#91;]&#91;] matrix = {{1.2, 4.3, 4.0}, \n      {4.1, -1.1}\n};<\/code><\/pre>\n\n\n\n<p>Here, we have created a multidimensional array named matrix. It is a 2-dimensional array.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn to work with arrays in Java. We will learn to declare, initialize, and access array elements with the help of examples. An array is a collection of similar types of data. For example, if we want to store the names of 100 people then we can create an array [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[427],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1174"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1174"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1174\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}