logo

Statische array op Java

Op Java, reeks is de belangrijkste datastructuur die elementen van hetzelfde type bevat. Het slaat elementen op in aaneengesloten geheugentoewijzing. Er zijn twee soorten arrays, d.w.z. statische reeks En dynamische reeks. In deze sectie zullen we ons alleen concentreren op statische array in Java .

Statische array

Een array die wordt gedeclareerd met het statische sleutelwoord staat bekend als statische array. Het wijst tijdens het compileren geheugen toe waarvan de grootte vast is. We kunnen de statische array niet wijzigen.

Als we willen dat de grootte van een array wordt bepaald op basis van de invoer van de gebruiker, kunnen we geen statische arrays gebruiken. In een dergelijk geval stellen dynamische arrays ons in staat de grootte van een array tijdens runtime te specificeren.

Voorbeeld van een statische array

Int arr[10] creƫert bijvoorbeeld een array van grootte 10. Dit betekent dat we slechts 10 elementen kunnen invoegen; we kunnen geen 11e element toevoegen omdat de grootte van Array vast staat.

clustering
 int arr[] = { 1, 3, 4 }; // static integer array int* arr = new int[3]; // dynamic integer array 

Voordelen van statische array

  • Het heeft een efficiĆ«nte uitvoeringstijd.
  • De levensduur van statische toewijzing is de gehele looptijd van het programma.

Nadelen van statische array

  • Als er meer statische gegevensruimte wordt gedeclareerd dan nodig is, is er sprake van ruimteverspilling.
  • Als er minder statische ruimte wordt gedeclareerd dan nodig is, wordt het onmogelijk om deze vaste grootte tijdens de looptijd uit te breiden.

Een statische array declareren

De syntaxis voor het declareren van een statische array is:

 []={,,.....}; 

Bijvoorbeeld:

 String[] suit = new String[] { 'Japan', 'India', 'Austria', 'Dubai' }; 

We kunnen een statische array ook als volgt declareren en initialiseren:

 String[] suit = { 'Japan', 'India', 'Austria', 'Dubai' }; 

Statische array kan ook als lijst worden gedeclareerd. Bijvoorbeeld:

c++ converteert in naar string
 List suit = Arrays.asList( 'Japan', 'India', 'Austria', 'Dubai' ); 

Static Array Java-programma

StaticArrayExample.java

 public class StaticArrayExample { private static String[] array; static { array = new String[2]; array[0] = &apos;Welcome to&apos;; array[1] = &apos;Javatpoint&apos;; } public static void main(String args[]) { for(int i = 0; i <array.length; i++) { system.out.print(array[i] + ' '); } < pre> <p> <strong>Output:</strong> </p> <pre> Welcome to Javatpoint </pre> <p>Let&apos;s see another Java program.</p> <p> <strong>StaticArrayExample.java</strong> </p> <pre> public class StaticArrayExample2 { //creates a static array of integer type static Integer[] integerArray; static { integerArray = new Integer[] { new Integer(1), new Integer(2), new Integer(3), new Integer(4), new Integer(5)}; } public static void main(String args[]) { //loop iterate over static array for (int i = 0; i <integerarray.length; i++) { prints array elements system.out.println(integerarray[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 5 </pre> <h2>Difference Between Static Array and Dynamic Array</h2> <p>The following table describes the key differences between static array and dynamic array.</p> <table class="table"> <tr> <th>Static Array</th> <th>Dynamic Array</th> </tr> <tr> <td>Static arrays are allocated memory at compile time.</td> <td>Dynamic array is located at run-time.</td> </tr> <tr> <td>The size of static array is fixed.</td> <td>The size of dynamic array is fixed. </td> </tr> <tr> <td>It is located in stack memory space.</td> <td>It is located in heap memory space.</td> </tr> <tr> <td>int array[10]; //array of size 10</td> <td>int* array = new int[10];</td> </tr> </table> <hr></integerarray.length;></pre></array.length;>

Laten we eens een ander Java-programma bekijken.

StaticArrayExample.java

 public class StaticArrayExample2 { //creates a static array of integer type static Integer[] integerArray; static { integerArray = new Integer[] { new Integer(1), new Integer(2), new Integer(3), new Integer(4), new Integer(5)}; } public static void main(String args[]) { //loop iterate over static array for (int i = 0; i <integerarray.length; i++) { prints array elements system.out.println(integerarray[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 5 </pre> <h2>Difference Between Static Array and Dynamic Array</h2> <p>The following table describes the key differences between static array and dynamic array.</p> <table class="table"> <tr> <th>Static Array</th> <th>Dynamic Array</th> </tr> <tr> <td>Static arrays are allocated memory at compile time.</td> <td>Dynamic array is located at run-time.</td> </tr> <tr> <td>The size of static array is fixed.</td> <td>The size of dynamic array is fixed. </td> </tr> <tr> <td>It is located in stack memory space.</td> <td>It is located in heap memory space.</td> </tr> <tr> <td>int array[10]; //array of size 10</td> <td>int* array = new int[10];</td> </tr> </table> <hr></integerarray.length;>

Verschil tussen statische array en dynamische array

De volgende tabel beschrijft de belangrijkste verschillen tussen statische array en dynamische array.

Statische array Dynamische array
Aan statische arrays wordt tijdens het compileren geheugen toegewezen. Dynamische array bevindt zich tijdens runtime.
De grootte van de statische array staat vast. De grootte van de dynamische array staat vast.
Het bevindt zich in de stapelgeheugenruimte. Het bevindt zich in de heap-geheugenruimte.
int-array[10]; //array van grootte 10 int* array = nieuwe int[10];