فيما يلي امثلة على التعامل مع المصفوفات في لغة جافا java

في البداية, المصفوفة هي عبارة عن بنية معطيات مهمتها تخزين عدة قيم بمكان واحد بالذاكرة, بعكس المتحول العادي الذي تستطيع تخزين قيمة واحدة فيه فقط.

 

مثال مصفوفة اسماء حيوانات وطباعتها:

class Main {
  public static void main(String[] args) {

String []animals=new String[4];
animals[0]="dog";
animals[1]="cat";
animals[2]="elephant";
animals[3]="bird";

//print the first element of array:
System.out.println(animals[0]);

//for loop to print all elements of animals array
for(int i =0;i<animals.length;i++)
{
System.out.println(animals[i]);
}

}
}

 

برنامج يطلب من المستخدم ادخال 10 ارقام  ويخزنهم في مصفوفة احادية الابعاد, ومن ثم يعرض عناصر المصفوفة, ويعرض اعلى رقم واقل رقم ومجموع الارقام ومتوسط الارقام:

//write java program that asks user to enter 10 numbers, and store it in array.
        //then displays the array elements, highest number, lowest number, summation of numbers and average of numbers.
        Scanner input=new Scanner(System.in);
         int []numbers=new int[10];
         int total = 0;
         int highest_number = 0;
         int lowest_number = 999999;
        for(int i=0;i<10; i++)
        {
            System.out.println("Please enter number: ");
            numbers[i] = input.nextInt();
            total = total+numbers[i];
            if(numbers[i]>highest_number)
                highest_number=numbers[i];
            if(numbers[i]<lowest_number)
                lowest_number=numbers[i];
        }
            System.out.println("Summation of numbers is: " + total);
            System.out.println("Average of numbers is: " + total/10 );
            System.out.println("Highest number is: " + highest_number);
            System.out.println("Lowest number is: " + lowest_number);

 

مثال لانشاء مصفوفة من اربع عناصر تحوي اسماء الحيوانات:

//write java program: create an array of 4 elements called "animals" and put the names of animals inside it.
  
            //solution 1: using the assignment statement directly.
         String []animals1= {"dog","cat","elephant","bird"};
  
//solution 2: assign the values later.
String []animals2=new String[4];
animals2[0]="dog";
animals2[1]="cat";
animals2[2]="elephant";
animals2[3]="bird";
  

//solution 3: using loops
  
Scanner input=new Scanner(System.in);
String []animals3=new String[4];
for(int i =0; i<4; i++)
{
    System.out.println("Please enter an animal name: ");
    animals3[i] = input.next();
    System.out.println(animals3[i]);
}

 

 

برنامج يستخدم المصفوفات ثنائية الابعاد 2-dimensional arrays

//write java program that prompts user to enter the students names and specialties, then store it in a 2-dimensional array,
    //the program should ask user about the number of students.
    //finally print the elements of the array in a nice way.
    Scanner input=new Scanner(System.in);
    System.out.println("how many students you have ?");
    int numberofstudents=input.nextInt();
    String [][]students;
    students=new String[2][numberofstudents];
    for(int column=0;column

 

برنامج لادارة وتخزين بيانات الموظفين في شركة, ضمن مصفوفة لاسماء الموظفين

import java.util.Scanner;
 
public class Main{

    public static void main(String[] args) {

//برنامج لادارة وتخزين بيانات الموظفين في شركة, ضمن مصفوفة لاسماء الموظفين
 Scanner input=new Scanner(System.in);
    int number_of_employees;
        System.out.print("How many employee SSNs you will enter?");
        number_of_employees=input.nextInt();
        
        String[] employees_names=new String[number_of_employees];
  //entering employees data
  for (int i = 0; i<number_of_employees;i++)
        {
        System.out.print("enter name of employee no."+(i+1));
        employees_names[i]=input.next();
        } 
        //printing employees names:
  for (int j = 0; j<employees_names.length;j++)
        {
    System.out.println(employees_names[j]);
        } 
  }
}

ابحث عن مسائل برمجة جافا | Java programming بالانجليزي

هل أعجبك المحتوى؟

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!

التعليقات
لا يوجد تعليقات
لاضافة سؤال او تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول