更新時(shí)間:2022-09-29 11:25:57 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽3314次
在Java中,數(shù)組長(zhǎng)度是數(shù)組可以容納的元素?cái)?shù)。沒(méi)有預(yù)定義的方法來(lái)獲取數(shù)組的長(zhǎng)度。我們可以使用數(shù)組屬性長(zhǎng)度在Java中找到數(shù)組長(zhǎng)度。我們將此屬性與數(shù)組名一起使用。在本節(jié)中,我們將學(xué)習(xí)如何在Java中查找數(shù)組的長(zhǎng)度或大小。
Java提供了一個(gè)屬性長(zhǎng)度,用于確定數(shù)組的長(zhǎng)度。每個(gè)數(shù)組都有一個(gè)內(nèi)置長(zhǎng)度屬性,其值為數(shù)組的大小。Size表示數(shù)組可以包含的元素總數(shù)??梢允褂命c(diǎn)(.)運(yùn)算符后跟數(shù)組名稱(chēng)來(lái)調(diào)用length屬性。我們可以找到int[]、double[]、String[]等的長(zhǎng)度。例如:
int[] arr=new int[5];
int arrayLength=arr.length
在上面的代碼片段中,arr是一個(gè)int類(lèi)型的數(shù)組,可以容納5個(gè)元素。arrayLength是存儲(chǔ)數(shù)組長(zhǎng)度的變量。為了找到數(shù)組的長(zhǎng)度,我們分別使用了數(shù)組名(arr),后跟點(diǎn)運(yùn)算符和長(zhǎng)度屬性。它決定數(shù)組的大小。

請(qǐng)注意,長(zhǎng)度決定了數(shù)組可以包含的最大元素?cái)?shù)或數(shù)組的容量。它不計(jì)算插入數(shù)組的元素。也就是說(shuō),length返回?cái)?shù)組的總大小。對(duì)于其元素在創(chuàng)建時(shí)初始化的數(shù)組,長(zhǎng)度和大小是相同的。
如果我們討論邏輯大小,數(shù)組的索引,那么只需intarrayLength=arr。長(zhǎng)度為-1,因?yàn)閿?shù)組索引從0開(kāi)始。因此,邏輯或數(shù)組索引總是小于實(shí)際大小1。

讓我們通過(guò)一個(gè)例子來(lái)找到數(shù)組的長(zhǎng)度。
例子1
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
例子2
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
例子3
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
相關(guān)閱讀
Java實(shí)驗(yàn)班
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
Java就業(yè)班
有基礎(chǔ) 直達(dá)就業(yè)
Java夜校直播班
業(yè)余時(shí)間 高薪轉(zhuǎn)行
Java在職加薪班
工作1~3年,加薪神器
Java架構(gòu)師班
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話(huà)與您溝通安排學(xué)習(xí)