{"id":1110,"date":"2022-02-07T19:13:10","date_gmt":"2022-02-07T19:13:10","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1110"},"modified":"2022-02-07T19:13:10","modified_gmt":"2022-02-07T19:13:10","slug":"arrays","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/07\/arrays\/","title":{"rendered":"Arrays"},"content":{"rendered":"\n<p>Java provides a data structure, the&nbsp;<strong>array<\/strong>, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.<\/p>\n\n\n\n<p>Instead of declaring individual variables, such as number0, number1, &#8230;, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and &#8230;, numbers[99] to represent individual variables.<\/p>\n\n\n\n<p>This tutorial introduces how to declare array variables, create arrays, and process arrays using indexed variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"declaring-array-variables\">Declaring Array Variables<\/h2>\n\n\n\n<p>To use an array in a program, you must declare a variable to reference the array, and you must specify the type of array the variable can reference. Here is the syntax for declaring an array variable \u2212<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"syntax\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>dataType&#91;] arrayRefVar;   \/\/ preferred way.\nor\ndataType arrayRefVar&#91;];  \/\/ works but not preferred way.\n<\/code><\/pre>\n\n\n\n<p><strong>Note<\/strong>&nbsp;\u2212 The style&nbsp;<strong>dataType[] arrayRefVar<\/strong>&nbsp;is preferred. The style&nbsp;<strong>dataType arrayRefVar[]<\/strong>&nbsp;comes from the C\/C++ language and was adopted in Java to accommodate C\/C++ programmers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example<\/h3>\n\n\n\n<p>The following code snippets are examples of this syntax \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>double&#91;] myList;   \/\/ preferred way.\nor\ndouble myList&#91;];   \/\/ works but not preferred way.<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-arrays\">Creating Arrays<\/h2>\n\n\n\n<p>You can create an array by using the new operator with the following syntax \u2212<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"syntax\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>arrayRefVar = new dataType&#91;arraySize];\n<\/code><\/pre>\n\n\n\n<p>The above statement does two things \u2212<\/p>\n\n\n\n<ul><li>It creates an array using new dataType[arraySize].<\/li><li>It assigns the reference of the newly created array to the variable arrayRefVar.<\/li><\/ul>\n\n\n\n<p>Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combined in one statement, as shown below \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dataType&#91;] arrayRefVar = new dataType&#91;arraySize];\n<\/code><\/pre>\n\n\n\n<p>Alternatively you can create arrays as follows \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dataType&#91;] arrayRefVar = {value0, value1, ..., valuek};\n<\/code><\/pre>\n\n\n\n<p>The array elements are accessed through the&nbsp;<strong>index<\/strong>. Array indices are 0-based; that is, they start from 0 to&nbsp;<strong>arrayRefVar.length-1<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example<\/h3>\n\n\n\n<p>Following statement declares an array variable, myList, creates an array of 10 elements of double type and assigns its reference to myList \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>double&#91;] myList = new double&#91;10];<\/code><\/pre>\n\n\n\n<p>Following picture represents array myList. Here, myList holds ten double values and the indices are from 0 to 9.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tutorialspoint.com\/java\/images\/java_array.jpg\" alt=\"Java Array\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"processing-arrays\">Processing Arrays<\/h2>\n\n\n\n<p>When processing array elements, we often use either&nbsp;<strong>for<\/strong>&nbsp;loop or&nbsp;<strong>foreach<\/strong>&nbsp;loop because all of the elements in an array are of the same type and the size of the array is known.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example<\/h3>\n\n\n\n<p>Here is a complete example showing how to create, initialize, and process arrays \u2212<a href=\"http:\/\/tpcg.io\/aXdX5u\" target=\"_blank\" rel=\"noreferrer noopener\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class TestArray {\n\n   public static void main(String&#91;] args) {\n      double&#91;] myList = {1.9, 2.9, 3.4, 3.5};\n\n      \/\/ Print all the array elements\n      for (int i = 0; i &lt; myList.length; i++) {\n         System.out.println(myList&#91;i] + \" \");\n      }\n     \n      \/\/ Summing all elements\n      double total = 0;\n      for (int i = 0; i &lt; myList.length; i++) {\n         total += myList&#91;i];\n      }\n      System.out.println(\"Total is \" + total);\n      \n      \/\/ Finding the largest element\n      double max = myList&#91;0];\n      for (int i = 1; i &lt; myList.length; i++) {\n         if (myList&#91;i] &gt; max) max = myList&#91;i];\n      }\n      System.out.println(\"Max is \" + max);  \n   }\n}<\/code><\/pre>\n\n\n\n<p>This will produce the following result \u2212<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"output\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">1.9\n2.9\n3.4\n3.5\nTotal is 11.7\nMax is 3.5\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-foreach-loops\">The foreach Loops<\/h2>\n\n\n\n<p>JDK 1.5 introduced a new for loop known as foreach loop or enhanced for loop, which enables you to traverse the complete array sequentially without using an index variable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example<\/h3>\n\n\n\n<p>The following code displays all the elements in the array myList \u2212Live Demo<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class TestArray {\n\n   public static void main(String&#91;] args) {\n      double&#91;] myList = {1.9, 2.9, 3.4, 3.5};\n\n      \/\/ Print all the array elements\n      for (double element: myList) {\n         System.out.println(element);\n      }\n   }\n}<\/code><\/pre>\n\n\n\n<p>This will produce the following result \u2212<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"output\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">1.9\n2.9\n3.4\n3.5\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"passing-arrays-to-methods\">Passing Arrays to Methods<\/h2>\n\n\n\n<p>Just as you can pass primitive type values to methods, you can also pass arrays to methods. For example, the following method displays the elements in an&nbsp;<strong>int<\/strong>&nbsp;array \u2212<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void printArray(int&#91;] array) {\n   for (int i = 0; i &lt; array.length; i++) {\n      System.out.print(array&#91;i] + \" \");\n   }\n}<\/code><\/pre>\n\n\n\n<p>You can invoke it by passing an array. For example, the following statement invokes the printArray method to display 3, 1, 2, 6, 4, and 2 \u2212<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>printArray(new int&#91;]{3, 1, 2, 6, 4, 2});<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"returning-an-array-from-a-method\">Returning an Array from a Method<\/h2>\n\n\n\n<p>A method may also return an array. For example, the following method returns an array that is the reversal of another array \u2212<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public static int&#91;] reverse(int&#91;] list) {\n   int&#91;] result = new int&#91;list.length];\n\n   for (int i = 0, j = result.length - 1; i &lt; list.length; i++, j--) {\n      result&#91;j] = list&#91;i];\n   }\n   return result;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-arrays-class\">The Arrays Class<\/h2>\n\n\n\n<p>The java.util.Arrays class contains various static methods for sorting and searching arrays, comparing arrays, and filling array elements. These methods are overloaded for all primitive types.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Sr.No.<\/th><th>Method &amp; Description<\/th><\/tr><tr><td>1<\/td><td><strong>public static int binarySearch(Object[] a, Object key)<\/strong>Searches the specified array of Object ( Byte, Int , double, etc.) for the specified value using the binary search algorithm. The array must be sorted prior to making this call. This returns index of the search key, if it is contained in the list; otherwise, it returns ( \u2013 (insertion point + 1)).<\/td><\/tr><tr><td>2<\/td><td><strong>public static boolean equals(long[] a, long[] a2)<\/strong>Returns true if the two specified arrays of longs are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. This returns true if the two arrays are equal. Same method could be used by all other primitive data types (Byte, short, Int, etc.)<\/td><\/tr><tr><td>3<\/td><td><strong>public static void fill(int[] a, int val)<\/strong>Assigns the specified int value to each element of the specified array of ints. The same method could be used by all other primitive data types (Byte, short, Int, etc.)<\/td><\/tr><tr><td>4<\/td><td><strong>public static void sort(Object[] a)<\/strong>Sorts the specified array of objects into an ascending order, according to the natural ordering of its elements. The same method could be used by all other primitive data types ( Byte, short, Int, etc.)<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Java provides a data structure, the&nbsp;array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[238],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1110"}],"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=1110"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1110\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}