logo

جافا تهيئة المصفوفة

جافا تهيئة المصفوفة هو في الأساس مصطلح يستخدم لتهيئة مصفوفة في Java. نحن نعلم أن المصفوفة عبارة عن مجموعة من أنواع البيانات المتشابهة. المصفوفة عبارة عن بنية بيانات مهمة جدًا تستخدم لحل مشكلات البرمجة.

الكلمة عنصر يستخدم للقيم المخزنة في مواضع مختلفة من المصفوفة. من أجل استخدام بنية بيانات المصفوفة في الكود الخاص بنا، نعلن عنها أولاً، وبعد ذلك، نقوم بتهيئتها.

إعلان مصفوفة

بناء جملة الإعلان عن المصفوفة في جافا ويرد أدناه.

 datatype [] 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;>