計(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)文章
探索HttpClient中的close方法及其對(duì)連接的影響
這篇文章主要為大家介紹了HttpClient中的close方法及其對(duì)連接的影響探索分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
Spring Cloud Gateway Hystrix fallback獲取異常信息的處理
這篇文章主要介紹了Spring Cloud Gateway Hystrix fallback獲取異常信息的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java正則表達(dá)式匹配字符串并提取中間值的方法實(shí)例
正則表達(dá)式常用于字符串處理、表單驗(yàn)證等場(chǎng)合,實(shí)用高效,下面這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式匹配字符串并提取中間值的相關(guān)資料,需要的朋友可以參考下2022-06-06
Spring注解驅(qū)動(dòng)之關(guān)于@Bean注解指定初始化和銷毀的方法
這篇文章主要介紹了Spring注解驅(qū)動(dòng)之關(guān)于@Bean注解指定初始化和銷毀的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
如何動(dòng)態(tài)改變Retrofit的base url和rest版本詳解
這篇文章主要給大家介紹了關(guān)于如何動(dòng)態(tài)改變Retrofit的base url和rest版本的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
解決CollectionUtils.isNotEmpty()不存在的問(wèn)題
這篇文章主要介紹了解決CollectionUtils.isNotEmpty()不存在的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02

