إيجاد اليونين بين مصفوفتين

عزّام العُمري • منذ 6 سنوات

السلام عليكم ورحمة الله

يا أخوان واجهة مشكلة في سؤال معين ..

 

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

الجزء الأول ضبط معي الحمد لله، بس الثاني ما عرفت له.

 

مرفق السؤال والكود اللي وصلت له وشكرًا لكم.

 

Question 03: Write a Java main method that calls a method readArray two times to read integer values in two integer arrays each of size input in the readArray method. Your readArray method must validate the size of an array by looping until a size that is not negative is entered. Assume the values read in each array are distinct [i.e., in each array there are no duplicate values. DON’T TEST FOR THIS] and that there are no InputMismatchExceptions. The main method then passes the two array references to a method getSetUnion which returns a reference to an integer array that contains the union of the two arrays. The main method finally displays the union.

Note:

  • Your program must be general.
  • The returned set union must have distinct elements.
  • The method getSetUnion must not contain calls to input or output methods.
  • The behaviour of your program must be similar to the program runs below:

 

import java.util.Scanner;

public class Q3 {	
	
	public static int[] readArray(int array[]) {
		
		Scanner reader = new Scanner(System.in);
		Boolean inputError;	
		int count = 0;

		do {
			System.out.print("Enter the size of the array: ");
			int size = reader.nextInt();

			if (size >= 0) {
				inputError = false;
				
				array = new int[size];				
				for (int k = 0; k < array.length; k++) {
					count++;
					System.out.print("Enter the element #" + count + ": ");
					array[k] = reader.nextInt();
				}
//				System.out.println(array.length);
			}
			else
				inputError = true;
			
		} while (inputError);
		return array;
	}
	
	//
	
	public static int getSetUnion(int[] firist, int[] second) {
	        //هنا المفترض تكون ميثود اليونيون
	      return 0;
	}
	
	public static void main(String[] args) {

		Scanner reader = new Scanner(System.in);

		int firistArray[] = readArray(null);
		int secondArray[] = readArray(null);
		
		getSetUnion(firistArray, secondArray);
		
		
	}

}

 

كلمات دليلية: arrays java

ساعد بالإجابة

"إن في قضاء حوائج الناس لذة لا يَعرفها إلا من جربها، فافعل الخير مهما استصغرته فإنك لا تدري أي حسنة تدخلك الجنة."

الإجابات (1)

Abdullah • منذ 6 سنوات

المشكلة عندة باليونيون ؟

اذا كانت كذلك فتحتاج يكون في عندك فنكشن تستقبل ال٢ اراي 

ويكون داخلها لوب يقارن بين العنصرين ويحفظه في اراي جديدة كمثال 

 

public static int getSetUnion(int[] firist, int[] second)

{

int [] newarr ;

(++for( i = 0 ; i < first.length() ; i

{

if (firist[i] == second[i]){

newarr[i] == firist[i]

}

}

return newarr; }

لايوجد لديك حساب في عالم البرمجة؟

تحب تنضم لعالم البرمجة؟ وتنشئ عالمك الخاص، تنشر المقالات، الدورات، تشارك المبرمجين وتساعد الآخرين، اشترك الآن بخطوات يسيرة !