logo

Java Initialiser le tableau

Tableau d'initialisation Java est essentiellement un terme utilisé pour initialiser un tableau en Java. Nous savons qu’un tableau est une collection de types de données similaires. Le tableau est une structure de données très importante utilisée pour résoudre des problèmes de programmation.

chaîne un entier

Le mot élément est utilisé pour les valeurs stockées dans différentes positions du tableau. Afin d'utiliser la structure de données Array dans notre code, nous la déclarons d'abord, puis nous l'initialisons.

Déclaration d'un tableau

La syntaxe de déclaration d'un tableau en Java est donné ci-dessous.

 datatype [] arrayName; 

Ici, le type de données est le type d'élément qui sera stocké dans le tableau, crochet[] est pour la taille du tableau, et nom_tableau est le nom du tableau.

Initialisation d'un tableau

Seule la déclaration du tableau n'est pas suffisante. Afin de stocker des valeurs dans le tableau, il est nécessaire de l'initialiser après la déclaration. La syntaxe d'initialisation d'un tableau est donnée ci-dessous.

 datatype [] arrayName = new datatype [ size ] 

En Java, il existe plusieurs façons d'initialiser un tableau :

1. Sans attribuer de valeurs

De cette façon, nous transmettons la taille au accolades [], et la valeur par défaut de chaque élément présent dans le tableau est 0. Prenons un exemple et comprenons comment initialiser un tableau sans attribuer de valeurs.

ArrayExample1.java

 public class ArrayExample1 { public static void main( String args[] ) { //initializing array without passing values int[] array = new int[5]; //print each element of the array for (int i = 0; i <5; i++) { system.out.println(array[i]); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/54/java-initialize-array.webp" alt="Java Initialize array"> <p> <strong>2. After the declaration of the array</strong> </p> <p>In this way, we initialize the array after the declaration of it. We use the <strong>new</strong> keyword assigning an array to a declared variable. Let&apos;s take an example and understand how we initialize an array after declaration.</p> <p> <strong>ArrayExample2.java</strong> </p> <pre> public class ArrayExample2 { //main() method start public static void main( String args[] ) { //declaration of an array int [] numbers; //initializing array after declaration numbers = new int[]{22,33,44,55,66}; //print each element of the array for (int i = 0; i <5; i++) { system.out.println(numbers[i]); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/54/java-initialize-array-2.webp" alt="Java Initialize array"> <h3>3. Initialize and assign values together</h3> <p>In this way, we declare and initialize the array together. We don&apos;t do both the declaration and initialization separately. Let&apos;s take an example and understand how we do both the thing together:</p> <p> <strong>ArrayExample3.java</strong> </p> <pre> public class ArrayExample3 { //main() method start public static void main( String args[] ) { //declaration of an array int [] numbers = {22,33,44,55,66}; //print each element of the array for (int i = 0; i <5; i++) { system.out.println(numbers[i]); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/54/java-initialize-array-3.webp" alt="Java Initialize array"> <p>All the above three ways are used based on the requirement of the functionality.</p> <hr></5;></pre></5;></pre></5;>