logo

Java inicializācijas masīvs

Java inicializācijas masīvs būtībā ir termins, ko izmanto masīva inicializācijai Java. Mēs zinām, ka masīvs ir līdzīgu datu veidu kopums. Masīvs ir ļoti svarīga datu struktūra, ko izmanto programmēšanas problēmu risināšanai.

Vārds elements tiek izmantots vērtībām, kas saglabātas dažādās masīva pozīcijās. Lai savā kodā izmantotu masīva datu struktūru, mēs vispirms to deklarējam un pēc tam inicializējam.

Masīva deklarācija

A deklarēšanas sintakse masīvs Java valodā ir norādīts zemāk.

 datatype [] arrayName; 

Šeit, datu tips ir elementa veids, kas tiks saglabāts masīvā, kvadrātiekava[] ir paredzēts masīva izmēram un masīvaNosaukums ir masīva nosaukums.

Masīva inicializācija

Nepietiek tikai ar masīva deklarāciju. Lai saglabātu vērtības masīvā, pēc deklarēšanas tas ir jāinicializē. Masīva inicializēšanas sintakse ir norādīta zemāk.

 datatype [] arrayName = new datatype [ size ] 

Java ir vairāk nekā viens masīva inicializācijas veids, kas ir šāds:

1. Bez vērtību piešķiršanas

Tādā veidā mēs nododam izmēru uz kvadrātveida lencēm [], un katra masīvā esošā elementa noklusējuma vērtība ir 0. Ņemsim piemēru un sapratīsim, kā inicializēt masīvu, nepiešķirot vērtības.

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