大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 Java學習 Java中查找數組的長度

Java中查找數組的長度

更新時間:2022-09-29 11:25:57 來源:動力節點 瀏覽2829次

在Java中,數組長度是數組可以容納的元素數。沒有預定義的方法來獲取數組的長度。我們可以使用數組屬性長度在Java中找到數組長度。我們將此屬性與數組名一起使用。在本節中,我們將學習如何在Java中查找數組的長度或大小。

數組長度屬性

Java提供了一個屬性長度,用于確定數組的長度。每個數組都有一個內置長度屬性,其值為數組的大小。Size表示數組可以包含的元素總數。可以使用點(.)運算符后跟數組名稱來調用length屬性。我們可以找到int[]、double[]、String[]等的長度。例如:

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

在上面的代碼片段中,arr是一個int類型的數組,可以容納5個元素。arrayLength是存儲數組長度的變量。為了找到數組的長度,我們分別使用了數組名(arr),后跟點運算符和長度屬性。它決定數組的大小。

請注意,長度決定了數組可以包含的最大元素數或數組的容量。它不計算插入數組的元素。也就是說,length返回數組的總大小。對于其元素在創建時初始化的數組,長度和大小是相同的。

如果我們討論邏輯大小,數組的索引,那么只需intarrayLength=arr。長度為-1,因為數組索引從0開始。因此,邏輯或數組索引總是小于實際大小1。

讓我們通過一個例子來找到數組的長度。

例子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

 

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 久久99国产视频 | 日本在线中文 | 欧美精品国产一区二区三区 | 日日摸天天摸狠狠摸视频 | 国产精品久久精品牛牛影视 | 伊人网伊人 | 亚洲欧美二区三区久本道 | 天天拍天天干 | 中文字幕在线欧美 | 九九热这里都是精品 | 亚洲欧洲一区二区 | 国产精品久久毛片蜜月 | 国产免费变态视频网址网站 | 草逼综合 | 国产伦精品一区二区三区免费观看 | 色涩网站在线观看 | 曰本性l交视频 | 欧美激情 亚洲 | 欧美日本在线视频 | 香蕉免费一区二区三区在线观看 | 特黄特黄一级高清免费大片 | 亚洲伦理一二三四 | 欧美videossex精品4k | 爱爱永久免费视频网站 | 伊人中文字幕在线 | 天天看天天射天天碰 | 亚洲精品96欧美一区二区 | 国产精品91av | 国产精品久久久久久久小唯西川 | 欧美成人午夜在线全部免费 | 最新国产精品亚洲二区 | 成人精品一区二区三区校园激情 | 欧美综合色区 | 亚洲七七久久综合桃花 | 欧日韩一区二区三区 | 四虎影院新地址 | 国产欧美精品一区aⅴ影院 国产欧美精品一区二区 | 国自产拍在线天天更新91 | 99精品视频在线视频免费观看 | 伊人久久国产 | 超碰人人操 |