logo

כיצד למצוא אורך מערך ב-Java

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

תכונת אורך מערך

Java מספק תכונה אורך שקובע את אורך של מערך . לכל מערך יש מובנה אורך מאפיין שערכו הוא גודל המערך. גודל מרמז על המספר הכולל של אלמנטים שמערך יכול להכיל. ניתן להפעיל את המאפיין length באמצעות ה- אופרטור נקודה (.). ואחריו שם המערך. נוכל למצוא את האורך של int[], double[], String[], וכו'. לדוגמה:

 int[] arr=new int[5]; int arrayLength=arr.length 

בקטע הקוד שלמעלה, arr הוא מערך מסוג int שיכול להכיל 5 אלמנטים. ה arrayLength הוא משתנה המאחסן את אורך המערך. כדי למצוא את אורך המערך, השתמשנו בשם המערך (arr) ואחריו באופרטור הנקודה ובתכונת האורך, בהתאמה. זה קובע את גודל המערך.

כיצד למצוא אורך מערך ב-Java

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

אם אנחנו מדברים על הגודל הלוגי, האינדקס של המערך, אז פשוט int arrayLength=arr.length-1 , כי אינדקס המערך מתחיל מ-0. לכן, האינדקס הלוגי או המערך יהיה תמיד קטן מהגודל האמיתי ב-1.

כיצד למצוא אורך מערך ב-Java

בואו נמצא את אורך המערך באמצעות דוגמה.

ArrayLengthExample1.java

 public class ArrayLengthExample1 { public static void main(String[] args) { //defining an array of type int named num //the square bracket contain the length of an array int[] num = new int[10]; //length is an Array attribute that determines the array length int arrayLength=num.length; //prints array length System.out.println('The length of the array is: '+ arrayLength); } } 

תְפוּקָה:

 The length of the array is: 10 

ArrayLengthExample2.java

 public class ArrayLengthExample2 { public static void main(String[] args) { //initializing an array of type String named country String[] country = { 'India', 'Australia', 'Japan', 'USA', 'UAE', 'Canada', 'Brazil'}; //length is an Array attribute that determines the array length int arrayLength=country.length; //prints array length System.out.println('The size of the array is: ' + arrayLength); } } 

תְפוּקָה:

 The size of the array is: 7 

ArrayLengthExample3.java

 public class ArrayLengthExample3 { private static void LengthOfArray(String[] array) { //checks array is empty or not if (array == null) { //if the array is empty prints the following statement System.out.println('The array is empty, can't be determined length.'); } else { //length attribute of the Array class determines the length of an array int arrayLength = array.length; //prints the array length System.out.println('The length of the array is: '+arrayLength); } } public static void main(String[] args) { String[] fruits = { 'Guava', 'Banana', 'Apple', 'Papaya', 'Melon', 'Strawberry'}; String[] alphabets = { 'm', 'p', 'k', 'l', 't' }; String[] numbers = { '12', '25', '63', '84', '90', '11', '54'}; //passing null value to the function LengthOfArray(null); //passing fruits array to the function LengthOfArray(fruits); //passing alphabets array to the function LengthOfArray(alphabets); //passing numbers array to the function LengthOfArray(numbers); } } 

תְפוּקָה:

 The array is empty, can't be determined length. The length of the array is: 6 The length of the array is: 5 The length of the array is: 7