Arrays
An array in Java is a fixed-size data structure that stores a collection of elements of the exact same data type in contiguous memory locations under a single variable name.
<p></p><h2><!--StartFragment--><p>Think of an array like a daily pill organizer box. The entire box is the array (the single variable), and each little compartment represents a specific position (index) where you can store exactly one item of the same type (a pill). Instead of creating 50 separate variables for 50 students' grades (e.g., <code data-path-to-node="1" data-index-in-node="306">grade1</code>, <code data-path-to-node="1" data-index-in-node="314">grade2</code>... <code data-path-to-node="1" data-index-in-node="324">grade50</code>), you simply create one array called <code data-path-to-node="1" data-index-in-node="369">grades</code> that holds all 50 values in an organized row.</p></h2><h1><b>Key Features :-</b></h1><h4><br></h4><h4>1. Compare</h4><h2><p><!--StartFragment--><b>The Concept:</b> Verifying if two distinct arrays are identical in every way.</p><p><b>The Theory:</b> A true comparison checks two specific criteria. First, both arrays must have the exact same length (the total number of available slots). Second, the elements at every corresponding position must match perfectly. Order strictly matters; if two arrays contain the exact same items but in a different sequence, they are not considered equal.<br></p></h2><h3>2. Fill</h3><h2><p><b>The Concept:</b> Mass-assigning a default or baseline value to an array.</p><p><b>The Theory:</b> Instead of manually visiting every single index to assign a value, the fill operation automatically populates all available positions in the array (or a specific designated range) with one uniform value. This is typically used for initializing a system, resetting a game board, or clearing out old data before a new process begins.<br></p></h2><h3>3. Sort</h3><h2><p><b>The Concept:</b> Organizing data into a sequential, predictable order.</p><p><b>The Theory:</b> Sorting physically rearranges the elements stored in the memory locations. For numbers, it organizes them from lowest to highest (ascending). For text, it arranges items alphabetically based on standard character encoding. Java handles this behind the scenes using highly optimized, complex mathematical algorithms so the programmer does not have to write the sorting logic manually.<br></p></h2><h3>4. Search</h3><h2><p><b>The Concept:</b> Finding the exact location (index) of a specific requested value.</p><p><b>The Theory:</b> Java uses an advanced algorithm known as a "binary search." Instead of scanning the array item by item from start to finish, it checks the exact middle of the array, determines if the target value is higher or lower, and instantly eliminates half of the remaining possibilities. </p><p><b>The critical rule:</b> Because of this "divide and conquer" strategy, the array must be perfectly sorted first. If you attempt a binary search on unsorted data, the mechanism breaks and will return incorrect results.<br></p></h2><h3>5. Copy</h3><h2><p><b>The Concept:</b> Duplicating data into a brand-new structural container in memory.</p><p><b>The Theory:</b> The fundamental limitation of an array is that its size is permanently fixed upon creation. Copying is the primary workaround for this limitation. It creates a completely independent clone of the original data. When copying, you can specify the size of the new array container. If you make the new container larger than the original, the extra slots are padded with default empty values. If you make it smaller, the data is simply truncated to fit the new capacity.</p><p><br></p></h2><h2></h2><h3><b>Advantages</b></h3><ul><li><p><b>Instant Access:</b> Because elements are stored sequentially in memory, you can instantly grab any value if you know its index position (called random access).</p></li><li><p><b>Cleaner Code:</b> Arrays allow you to process large amounts of data efficiently using loops (like a <code data-path-to-node="5,1,0" data-index-in-node="96">for</code> loop) rather than writing repetitive code for hundreds of individual variables.</p></li></ul><h4><b>Disadvantages</b></h4><ul><li><p><b>Size Rigidity:</b> Because the size is fixed at creation, you have to guess how much space you need in advance. If you guess too high, you waste computer memory. If you guess too low, your program might crash. <i>(Note: Java provides a different tool called <code data-path-to-node="7,0,0" data-index-in-node="251">ArrayList</code> to solve this specific problem).</i></p></li><li><p><b>Slower Insertions/Deletions:</b> If you want to insert a new element in the middle of an array, you have to manually shift all the subsequent elements down one spot to make room.</p></li></ul><p><b>Example</b></p><pre><!--StartFragment--><pre _ngcontent-ng-c2555737016="">import java.util.Arrays;
<br>class array<br>{<br>public static void main(String args[])<br>{<br> int a[]={40,10,30,20,50};<br> System.out.println("Original Array");<br> System.out.println(Arrays.toString(a));
<br> //sorting of an array
<br> Arrays.sort(a);<br> System.out.println("Sorted Array");<br> System.out.println(Arrays.toString(a));
<br><br> //search an element
<br> int index=Arrays.binarySearch(a,30);<br> System.out.println("30 is found at index ="+index);
<br><br> //copy the array
<br> int b[]=Arrays.copyOf(a,a.length);<br> System.out.println(Arrays.toString(b));
<br><br> //fill the array
<br> Arrays.fill(b,100);<br> System.out.println(Arrays.toString(b));
<br> <br> //compare two arrays
<br> Boolean result=Arrays.equals(a,b);<br> System.out.println("both arrays are eqyal?"+result);
<br> }
<br>}</pre></pre><p><br></p><!--StartFragment--><p><br></p><!--EndFragment--><!--EndFragment--><p></p>
