如何確定Java中數組的長度或大小?
In Java, one of the convenient ways to determine the length or size of an array is by using its length property. It counts the number of elements stored in an array and returns the count . Finding the length of an array is one of the common yet crucial operation as it is used to find the number of elements of an array, append new elements into it and retrieve stored items. This article ms toways length or size of an array.
Java Program to determine the length or size of an Array
以下範例將幫助我們了解如何找到陣列的大小。
Example 1
的中文翻譯為:範例 1
The following example illustrates the use of length property with an array of type integer.
import java.util.*; public class Example1 { public static void main(String[] args) { int[] aray = new int[5]; // declaring array of size 5 // initializing the array aray[0] = 11; aray[1] = 21; aray[2] = 13; aray[3] = 23; aray[4] = 30; // printing the elements of array System.out.println("Elements of the given array: "); for(int elm : aray) { System.out.print(elm + " "); } System.out.println(); // printing the length of array System.out.println("Length of the given array: " + aray.length); } }
Output
#Elements of the given array: 11 21 13 23 30 Length of the given array: 5
在上面的程式碼中,我們宣告了一個大小為5的數組,這意味著它可以儲存最多5個元素。然後,使用for-each循環,我們檢索了所有的元素,並且藉助length屬性確定了給定數組的大小。
Example 2
的中文翻譯為:範例2
In this example, we will declare and initialize a String array. Then, using a for-each loop, we will print its element. In the end, with the help length property, we determine the size of given array.
import java.util.*; public class Example2 { public static void main(String[] args) { // declaration and initialization of the array String[] aray = { "Tutorix", "Tutorials", "Point", "Simply", "Easy", "Learning" }; // printing the elements of array System.out.println("Elements of the given array: "); for(String elm : aray) { System.out.print(elm + " "); } System.out.println(); // printing the length of array System.out.println("Length of the given array: " + aray.length); } }
Output
#Elements of the given array: Tutorix Tutorials Point Simply Easy Learning Length of the given array: 6
Example 3
This is another example where we will find the size of an array without using any in-built property or method of Java.
方法
宣告並初始化兩個陣列。一個是字串數組,另一個是整數數組
然後,定義兩個整數類型的計數變數來儲存兩個陣列的元素數量。
Now, use the for-each loop to iterate and increment the counter variable by one with each iteration.
最後,列印結果並退出。
import java.util.*; public class Example3 { public static void main(String[] args) { // declaration and initialization of the arrays String[] aray1 = { "Tutorix", "Tutorials", "Point", "Simply", "Easy", "Learning" }; int[] aray2 = {58, 66, 74, 55, 62}; // initial counter variable to store the count of elements int countr1 = 0; int countr2 = 0; // printing the length of both arrays for(String elm : aray1) { countr1++; } for(int elm : aray2) { countr2++; } System.out.println("Length of the String array: " + countr1); System.out.println("Length of the integer array: " + countr2); } }
Output
#Length of the String array: 6 Length of the integer array: 5
結論
在本文中,我們透過範例程式學習了使用length屬性來確定給定數組的大小。此外,我們還討論了一種在不使用任何內建方法和屬性的情況下找到數組長度的方法。
以上是如何確定Java中數組的長度或大小?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

在使用IntelliJIDEAUltimate版本啟動Spring...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...

在使用TKMyBatis進行數據庫查詢時,如何優雅地獲取實體類變量名以構建查詢條件,是一個常見的難題。本文將針...
