logo

Tableau statique en Java

En Java, tableau est la structure de données la plus importante contenant des éléments du même type. Il stocke les éléments dans une allocation de mémoire contiguë. Il existe deux types de tableaux, à savoir tableau statique et tableau dynamique. Dans cette section, nous nous concentrerons uniquement sur tableau statique en Java .

Tableau statique

Un tableau déclaré avec le mot clé static est appelé tableau statique. Il alloue de la mémoire au moment de la compilation dont la taille est fixe. Nous ne pouvons pas modifier le tableau statique.

Si nous voulons qu'un tableau soit dimensionné en fonction des entrées de l'utilisateur, nous ne pouvons pas utiliser de tableaux statiques. Dans un tel cas, les tableaux dynamiques nous permettent de spécifier la taille d'un tableau au moment de l'exécution.

Exemple de tableau statique

Par exemple, int arr[10] crée un tableau de taille 10. Cela signifie que nous ne pouvons insérer que 10 éléments ; nous ne pouvons pas ajouter un 11ème élément car la taille du tableau est fixe.

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

Avantages du réseau statique

  • Il a un temps d’exécution efficace.
  • La durée de vie de l'allocation statique correspond à la durée totale d'exécution du programme.

Inconvénients du tableau statique

  • Si plus d’espace de données statiques est déclaré que nécessaire, il y a une perte d’espace.
  • Si moins d'espace statique est déclaré que nécessaire, il devient alors impossible d'étendre cette taille fixe pendant l'exécution.

Déclarer un tableau statique

La syntaxe pour déclarer un tableau statique est :

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

Par exemple:

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

Nous pouvons également déclarer et initialiser un tableau statique comme suit :

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

Un tableau statique peut également être déclaré sous forme de liste. Par exemple:

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

Programme Java de tableau statique

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

Voyons un autre programme 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;>

Différence entre un tableau statique et un tableau dynamique

Le tableau suivant décrit les principales différences entre un tableau statique et un tableau dynamique.

Tableau statique Tableau dynamique
Les tableaux statiques se voient allouer de la mémoire au moment de la compilation. Le tableau dynamique est localisé au moment de l'exécution.
La taille du tableau statique est fixe. La taille du tableau dynamique est fixe.
Il est situé dans l'espace mémoire de la pile. Il est situé dans l'espace mémoire tas.
tableau int[10]; //tableau de taille 10 int* tableau = nouveau int[10];