logo

إضافة عناصر إلى المصفوفة في Java

المصفوفة هي مجموعة من أنواع مماثلة من العناصر المخزنة في مواقع متجاورة في الذاكرة. الميزة الرئيسية للمصفوفة هي أنه يمكننا الوصول بشكل عشوائي إلى عناصر المصفوفة، في حين لا يمكن الوصول إلى عناصر القائمة المرتبطة بشكل عشوائي.

في جافا , المصفوفات هي أنواع بيانات قابلة للتغيير، أي أن حجم المصفوفة ثابت، ولا يمكننا إضافة عنصر جديد مباشرة إلى المصفوفة. ومع ذلك، هناك طرق مختلفة لإضافة عناصر إلى المصفوفة. لنفترض أن لدينا مصفوفة arr، ونحتاج إلى إضافة عناصر إليها. يمكننا استخدام الطرق التالية لإضافة عناصر إلى arr.

  1. عن طريق إنشاء مصفوفة بحجم أكبر من arr.
  2. باستخدام ArrayList
  3. عن طريق تحويل العنصر لضبط حجم arr.

دعونا نلقي نظرة داخلية على الطرق التي وصفناها.

إنشاء مصفوفة ذات حجم أكبر

لإضافة عناصر إلى مصفوفة جافا، يمكننا إنشاء مصفوفة أخرى أكبر حجمًا ونسخ جميع العناصر من مصفوفتنا إلى مصفوفة أخرى ووضع القيمة الجديدة في آخر المصفوفة التي تم إنشاؤها حديثًا. ومع ذلك، فهي ليست طريقة فعالة لإضافة عنصر إلى المصفوفة. في المثال أدناه، تمت إضافة العنصر 7 إلى المصفوفة arr بمساعدة المصفوفة المنشأة حديثًا newArr. النظر في المثال التالي.

 import java.util.Arrays; public class ArrayExample { public static void main(String[] args) { // TODO Auto-generated method stub int arr[] = {1,2,3,4,5,6}; int n = arr.length; int newArr[] = new int[n+1]; int value = 7; System.out.println(Arrays.toString(arr)); for(int i = 0; i<n; i++) { newarr[i]="arr[i];" } newarr[n]="value;" system.out.println(arrays.tostring(newarr)); < pre> <h3>Using ArrayList</h3> <p>We can use <a href="/java-arraylist">ArrayList</a> as the intermediate structure and add the elements into the ArrayList using the add () method. ArrayList is a data structure that allows us to dynamically add elements. However, we can convert the ArrayList to the array by using the toArray() method. Hence this process involves the following steps.</p> <ol class="points"> <li>Convert Array into ArrayList using asList() method.</li> <li>Add elements into the array list using the add() method.</li> <li>Convert the ArrayList again to the array using the toArray() method.</li> </ol> <p>Consider the following example.</p> <pre> import java.util.ArrayList; import java.util.Arrays; public class JavaAddElementUsingList { public static void main(String[] args) { // TODO Auto-generated method stub Integer arr[] = {1,2,3,4,5,6}; System.out.println(&apos;Array:&apos;+Arrays.toString(arr)); ArrayList arrayList = new ArrayList(Arrays.asList(arr)); arrayList.add(7); arr = arrayList.toArray(arr); System.out.println(&apos;Array after adding element: &apos;+Arrays.toString(arr)); } } </pre> <p> <strong>Output:</strong> </p> <pre> Array:[1, 2, 3, 4, 5, 6] Array after adding element: [1, 2, 3, 4, 5, 6, 7] </pre> <h3>Shifting elements to adjust the size of the array</h3> <p>In this method, we will add the elements to the specified index in the array. Likewise, the above two processes will use a new destination array with a larger size than the original array. However, it will be tricky to shift the destination array elements after copying all elements from the original array to destination array.</p> <p>In this method, we will,</p> <ol class="points"> <li>Create a new destination array with a larger size than the original array.</li> <li>Copy all the elements from the original array to the new destination array</li> <li>Shift the elements after the given index to the right until it reaches the end of the array.</li> <li>Insert the new element at the given index.</li> </ol> <p>Consider the following example in which we will add a specific value at the given index 3 in the original array using a destination array.</p> <pre> import java.util.Arrays; public class JavaAddElementArraySpecified { public static void main(String[] args) { Integer arr[] = {1,2,3,4,5,6}; int n = arr.length; int index = 3; System.out.println(&apos;Original Array: &apos;+Arrays.toString(arr)); Integer newArr[] = new Integer[n+1]; int j = 0; for(int i = 0; i<newarr.length; i++) { if(i="=index)" newarr[i]="7;" }else j++; } newarr[index]="7;" system.out.println('array after adding value: '+arrays.tostring(newarr)); < pre> <p> <strong>Output:</strong> </p> <pre> Original Array: [1, 2, 3, 4, 5, 6] Array after adding value: [1, 2, 3, 7, 4, 5, 6] </pre> <hr></newarr.length;></pre></n;>

انتاج:

 Array:[1, 2, 3, 4, 5, 6] Array after adding element: [1, 2, 3, 4, 5, 6, 7] 

تبديل العناصر لضبط حجم المصفوفة

في هذه الطريقة، سنقوم بإضافة العناصر إلى الفهرس المحدد في المصفوفة. وبالمثل، ستستخدم العمليتان المذكورتان أعلاه مصفوفة وجهة جديدة بحجم أكبر من المصفوفة الأصلية. ومع ذلك، سيكون من الصعب نقل عناصر المصفوفة الوجهة بعد نسخ جميع العناصر من المصفوفة الأصلية إلى المصفوفة الوجهة.

في هذه الطريقة سنقوم،

  1. قم بإنشاء مصفوفة وجهة جديدة بحجم أكبر من المصفوفة الأصلية.
  2. انسخ كافة العناصر من المصفوفة الأصلية إلى المصفوفة الوجهة الجديدة
  3. انقل العناصر بعد الفهرس المحدد إلى اليمين حتى تصل إلى نهاية المصفوفة.
  4. أدخل العنصر الجديد في الفهرس المحدد.

خذ بعين الاعتبار المثال التالي الذي سنضيف فيه قيمة محددة في الفهرس المحدد 3 في المصفوفة الأصلية باستخدام مصفوفة الوجهة.

 import java.util.Arrays; public class JavaAddElementArraySpecified { public static void main(String[] args) { Integer arr[] = {1,2,3,4,5,6}; int n = arr.length; int index = 3; System.out.println(&apos;Original Array: &apos;+Arrays.toString(arr)); Integer newArr[] = new Integer[n+1]; int j = 0; for(int i = 0; i<newarr.length; i++) { if(i="=index)" newarr[i]="7;" }else j++; } newarr[index]="7;" system.out.println(\'array after adding value: \'+arrays.tostring(newarr)); < pre> <p> <strong>Output:</strong> </p> <pre> Original Array: [1, 2, 3, 4, 5, 6] Array after adding value: [1, 2, 3, 7, 4, 5, 6] </pre> <hr></newarr.length;>