logo

מערך אתחול Java

מערך אתחול Java הוא בעצם מונח המשמש לאתחול מערך ב-Java. אנו יודעים שמערך הוא אוסף של סוגי נתונים דומים. המערך הוא מבנה נתונים חשוב מאוד המשמש לפתרון בעיות תכנות.

המילה אֵלֵמֶנט משמש עבור הערכים המאוחסנים במיקומים שונים של המערך. על מנת להשתמש במבנה הנתונים של Array בקוד שלנו, אנו מכריזים עליו תחילה, ולאחר מכן אנו מאתחלים אותו.

הכרזה על מערך

התחביר של הצהרת an מערך ב-Java מובא להלן.

 datatype [] arrayName; 

כאן, סוג הנתונים הוא סוג האלמנט שיישמר במערך, סוגר מרובע[] הוא עבור גודל המערך, ו arrayName הוא שם המערך.

אתחול מערך

רק ההצהרה של המערך אינה מספיקה. על מנת לאחסן ערכים במערך, נדרש לאתחל אותו לאחר ההכרזה. התחביר של אתחול מערך ניתן להלן.

 datatype [] arrayName = new datatype [ size ] 

ב-Java, יש יותר מדרך אחת לאתחל מערך שהיא כדלקמן:

1. בלי להקצות ערכים

בדרך זו, אנו מעבירים את הגודל ל- פלטה מרובעת [], וערך ברירת המחדל של כל אלמנט הקיים במערך הוא 0. ניקח דוגמה ונבין כיצד אנו מאתחלים מערך מבלי להקצות ערכים.

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