計(jì)算Java數(shù)組長(zhǎng)度函數(shù)的方法以及代碼分析
Java 中的數(shù)組可以包含多個(gè)元素,具體取決于對(duì)象的創(chuàng)建方式。為了讓用戶執(zhí)行不同的操作,必須知道數(shù)組的長(zhǎng)度。
數(shù)組長(zhǎng)度屬性:如何求出數(shù)組的長(zhǎng)度?
為了獲得 Java 數(shù)組長(zhǎng)度,我們需要使用“數(shù)組長(zhǎng)度屬性”,如下例所示:
/** * An Example to get the Array Length is Java */ public class ArrayLengthJava { public static void main(String[] args) { String[] myArray = { "I", "Love", "Music" }; int arrayLength = myArray.length; //array length attribute System.out.println("The length of the array is: " + arrayLength); } }
輸出
數(shù)組的長(zhǎng)度為:3
必須注意,Java Array Object沒(méi)有獲取其長(zhǎng)度的方法。
很多時(shí)候,我們不知道數(shù)組對(duì)象是如何創(chuàng)建的。對(duì)于這樣的程序,我們使用一個(gè)接收數(shù)組并打印長(zhǎng)度的函數(shù)。
/** * An Example to find the Java Array Length using a function */ public class ArrayLengthJava { private static void printArrayLength(String[] myArray) { if (myArray == null) //to check whether the array is empty or not { System.out.println("The length of the array can't be determined."); } else { int arrayLength = myArray.length; System.out.println("The length of the array is: " + arrayLength); } } public static void main(String[] args) { String[] JavaArray1 = { "I", "Love", "Music" }; String[] JavaArray2 = { "R", "S" }; String[] JavaArray3 = { "1", "2", "3", "4" }; String[] JavaArray4 = { "Java" }; printArrayLength(null); printArrayLength(JavaArray1); printArrayLength(JavaArray2); printArrayLength(JavaArray3); printArrayLength(JavaArray4); } }
輸出:
無(wú)法確定數(shù)組的長(zhǎng)度。 數(shù)組的長(zhǎng)度為:3 數(shù)組的長(zhǎng)度為:2 數(shù)組的長(zhǎng)度為:4 數(shù)組的長(zhǎng)度為:1
必須注意,在訪問(wèn)空對(duì)象或 null 對(duì)象的長(zhǎng)度字段時(shí),會(huì)引發(fā) NullPointerException。
在 Java 中使用數(shù)組長(zhǎng)度搜索值
數(shù)組長(zhǎng)度有許多有用的屬性,可以在編程時(shí)使用。在下面的示例中,我們使用數(shù)組的長(zhǎng)度來(lái)遍歷所有元素并確定是否存在特定值。
/** * An Example that uses Java Array Length to check if the array contains a * specific value. */ public class ArrayLengthJava { private static boolean arrayContainsValue(String[] myArray, String lookForValue) { if (myArray != null) { int arrayLength = myArray.length; for (int i = 0; i <= arrayLength - 1; i++) { String value = myArray[i]; if (value.equals(lookForValue)) { return true; } } } return false; } public static void main(String[] args) { String[] JavaArray = { "I", "Love", "Music" }; System.out.println(arrayContainsValue(JavaArray, "Love")); System.out.println(arrayContainsValue(JavaArray, "Guitar")); } }
輸出:
真的 錯(cuò)誤的
上面給出的程序?qū)⒅递敵鰹檎?,因?yàn)?ldquo; Love”存在于數(shù)組中,而“吉他”是不存在的元素,因此輸出為假。
知識(shí)點(diǎn)擴(kuò)展:
動(dòng)態(tài)改變數(shù)組的長(zhǎng)度
/** * Reallocates an array with a new size, and copies the contents * * of the old array to the new array. * * @param oldArray the old array, to be reallocated. * * @param newSize the new array size. * * @return A new array with the same contents. * */ private static Object resizeArray (Object oldArray, int newSize) { int oldSize = java.lang.reflect.Array.getLength(oldArray); Class elementType = oldArray.getClass().getComponentType(); Object newArray = java.lang.reflect.Array.newInstance( elementType,newSize); int preserveLength = Math.min(oldSize,newSize); if (preserveLength > 0) System.arraycopy (oldArray,0,newArray,0,preserveLength); return newArray; } // Test routine for resizeArray(). public static void main (String[] args) { int[] a = {1,2,3}; a = (int[])resizeArray(a,5); a[3] = 4; a[4] = 5; for (int i=0; i<a.length; i++) System.out.println (a[i]); }
到此這篇關(guān)于計(jì)算Java數(shù)組長(zhǎng)度函數(shù)的方法以及代碼分析的文章就介紹到這了,更多相關(guān)計(jì)算Java數(shù)組長(zhǎng)度函數(shù)的方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java數(shù)組(Array)最全匯總(中篇)
- Java數(shù)組(Array)最全匯總(上篇)
- Java之?dāng)?shù)組在指定位置插入元素實(shí)現(xiàn)
- Java自定義一個(gè)變長(zhǎng)數(shù)組的思路與代碼
- Java中如何將?int[]?數(shù)組轉(zhuǎn)換為?ArrayList(list)
- Java中將 int[] 數(shù)組 轉(zhuǎn)換為 List分享
- java如何將int數(shù)組轉(zhuǎn)化為Integer數(shù)組
- 淺談Java當(dāng)作數(shù)組的幾個(gè)應(yīng)用場(chǎng)景
- Java C++題解leetcode915分割數(shù)組示例
- Java?從json提取數(shù)組并轉(zhuǎn)換為list的操作方法
- Java數(shù)據(jù)結(jié)構(gòu)之稀疏數(shù)組的實(shí)現(xiàn)與應(yīng)用
- Java?C++題解leetcode1441用棧操作構(gòu)建數(shù)組示例
- Java postgresql數(shù)組字段類型處理方法詳解
- Java中數(shù)組的常見(jiàn)操作合集
- 關(guān)于Java?SE數(shù)組的深入理解
- Java二維數(shù)組與稀疏數(shù)組相互轉(zhuǎn)換實(shí)現(xiàn)詳解
- Java數(shù)組隊(duì)列及環(huán)形數(shù)組隊(duì)列超詳細(xì)講解
- Java數(shù)組(Array)最全匯總(下篇)
相關(guān)文章
springboot實(shí)現(xiàn)配置本地訪問(wèn)端口及路徑
這篇文章主要介紹了springboot實(shí)現(xiàn)配置本地訪問(wèn)端口及路徑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java+Selenium調(diào)用JavaScript的方法詳解
這篇文章主要為大家講解了java在利用Selenium操作瀏覽器網(wǎng)站時(shí)候,有時(shí)會(huì)需要用的JavaScript的地方,代碼該如何實(shí)現(xiàn)呢?快跟隨小編一起學(xué)習(xí)一下吧2023-01-01xxl-job定時(shí)任務(wù)配置應(yīng)用及添加到springboot項(xiàng)目中實(shí)現(xiàn)動(dòng)態(tài)API調(diào)用
XXL-JOB是一個(gè)分布式任務(wù)調(diào)度平臺(tái),其核心設(shè)計(jì)目標(biāo)是開(kāi)發(fā)迅速、學(xué)習(xí)簡(jiǎn)單、輕量級(jí)、易擴(kuò)展,本篇文章主要是對(duì)xuxueli的xxl-job做一個(gè)簡(jiǎn)單的配置,以及將其添加到自己已有的項(xiàng)目中進(jìn)行api調(diào)用,感興趣的朋友跟隨小編一起看看吧2024-04-04教你利用JAVA實(shí)現(xiàn)可以自行關(guān)閉服務(wù)器的方法
今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著利用JAVA實(shí)現(xiàn)可以自行關(guān)閉服務(wù)器的方法展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06java設(shè)計(jì)模式:建造者模式之生產(chǎn)線
這篇文章主要介紹了Java設(shè)計(jì)模式之建造者模式,結(jié)合具體實(shí)例形式分析了建造者模式的概念、原理、實(shí)現(xiàn)方法與相關(guān)使用注意事項(xiàng),需要的朋友可以參考下2021-08-08新手初學(xué)Java對(duì)象內(nèi)存構(gòu)成
這篇文章主要介紹了深入理解JVM之Java對(duì)象的創(chuàng)建、內(nèi)存布局、訪問(wèn)定位,結(jié)合實(shí)例形式詳細(xì)分析了Java對(duì)象的創(chuàng)建、內(nèi)存布局、訪問(wèn)定位相關(guān)概念、原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下2021-07-07