logo

Array statico in Java

A Giava, vettore è la struttura dati più importante che contiene elementi dello stesso tipo. Memorizza gli elementi in un'allocazione di memoria contigua. Esistono due tipi di array, ad es. matrice statica E matrice dinamica. In questa sezione ci concentreremo solo su array statico in Java .

Matrice statica

Un array dichiarato con la parola chiave static è noto come array statico. Alloca memoria in fase di compilazione la cui dimensione è fissa. Non possiamo alterare l'array statico.

Se vogliamo che un array venga dimensionato in base all'input dell'utente, non possiamo utilizzare array statici. In tal caso, gli array dinamici ci consentono di specificare la dimensione di un array in fase di esecuzione.

Esempio di array statico

Ad esempio, int arr[10] crea un array di dimensione 10. Ciò significa che possiamo inserire solo 10 elementi; non possiamo aggiungere un undicesimo elemento poiché la dimensione di Array è fissa.

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

Vantaggi dell'array statico

  • Ha tempi di esecuzione efficienti.
  • La durata dell'allocazione statica corrisponde all'intero tempo di esecuzione del programma.

Svantaggi dell'array statico

  • Nel caso in cui venga dichiarato più spazio dati statico del necessario, si verifica uno spreco di spazio.
  • Nel caso in cui venga dichiarato meno spazio statico del necessario, diventa impossibile espandere questa dimensione fissa durante il runtime.

Dichiarazione di un array statico

La sintassi per dichiarare un array statico è:

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

Per esempio:

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

Possiamo anche dichiarare e inizializzare un array statico come segue:

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

L'array statico può anche essere dichiarato come List. Per esempio:

 List suit = Arrays.asList( 'Japan', 'India', 'Austria', 'Dubai' ); 

Programma Java per array statici

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;>

Vediamo un altro programma Java.

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;>

Differenza tra array statico e array dinamico

La tabella seguente descrive le principali differenze tra array statico e array dinamico.

Matrice statica Matrice dinamica
Agli array statici viene allocata memoria in fase di compilazione. L'array dinamico si trova in fase di esecuzione.
La dimensione dell'array statico è fissa. La dimensione dell'array dinamico è fissa.
Si trova nello spazio di memoria dello stack. Si trova nello spazio di memoria heap.
int array[10]; //array di dimensione 10 int* array = nuovo int[10];