Java數(shù)組進(jìn)階操作方法的核心技巧
前言
數(shù)組作為 Java 中最基礎(chǔ)的數(shù)據(jù)結(jié)構(gòu),其固定長度的特性使得增刪操作需要特殊處理。本文將基于數(shù)組查找功能,一步步實現(xiàn)元素插入(單個元素 / 數(shù)組)和刪除操作,帶你掌握數(shù)組操作的核心技巧。
基礎(chǔ):數(shù)組元素查找
首先,我們需要實現(xiàn)一個基礎(chǔ)功能 —— 查找指定元素在數(shù)組中的位置。這是后續(xù)所有操作的前提。
查找功能實現(xiàn)
/**
* 查找數(shù)組中指定元素的位置
*
* @param arr 待查找的數(shù)組
* @param target 要查找的目標(biāo)元素
* @return 元素所在索引,未找到返回-1
*/
public static int findElement(int[] arr, int target) {
// 空數(shù)組直接返回-1
if (arr == null || arr.length == 0) {
return -1;
}
// 遍歷數(shù)組查找元素
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i; // 找到元素,返回索引
}
}
return -1; // 未找到元素
}
進(jìn)階操作一:在指定元素后插入單個元素
代碼實現(xiàn)
/**
* 在指定元素后插入單個新元素
*
* @param arr 原數(shù)組
* @param target 目標(biāo)元素(在其后插入)
* @param newValue 要插入的新元素
* @return 插入后的新數(shù)組
*/
public static int[] insertAfterElement(int[] arr, int target, int newValue) {
int targetIndex = findElement(arr, target);
// 未找到目標(biāo)元素,返回原數(shù)組副本
if (targetIndex == -1) {
int[] newArr = new int[arr.length];
System.arraycopy(arr, 0, newArr, 0, arr.length);
return newArr;
}
// 創(chuàng)建新數(shù)組,長度+1
int[] newArr = new int[arr.length + 1];
// 復(fù)制目標(biāo)元素及之前的元素
System.arraycopy(arr, 0, newArr, 0, targetIndex + 1);
// 插入新元素
newArr[targetIndex + 1] = newValue;
// 復(fù)制目標(biāo)元素之后的元素
System.arraycopy(arr, targetIndex + 1, newArr, targetIndex + 2,
arr.length - targetIndex - 1);
return newArr;
}
進(jìn)階操作二:在指定元素后插入數(shù)組
有時候我們需要插入多個元素,這就需要在指定位置插入一個數(shù)組。
代碼實現(xiàn)
/**
* 在指定元素后插入一個數(shù)組的所有元素
*
* @param arr 原數(shù)組
* @param target 目標(biāo)元素(在其后插入)
* @param insertArr 要插入的數(shù)組
* @return 插入后的新數(shù)組
*/
public static int[] insertArrayAfterElement(int[] arr, int target, int[] insertArr) {
if (insertArr == null || insertArr.length == 0) {
return arr.clone(); // 插入數(shù)組為空,返回原數(shù)組副本
}
int targetIndex = findElement(arr, target);
// 未找到目標(biāo)元素,返回原數(shù)組副本
if (targetIndex == -1) {
return arr.clone();
}
// 創(chuàng)建新數(shù)組,長度=原數(shù)組長度+插入數(shù)組長度
int[] newArr = new int[arr.length + insertArr.length];
// 復(fù)制目標(biāo)元素及之前的元素
System.arraycopy(arr, 0, newArr, 0, targetIndex + 1);
// 插入新數(shù)組
System.arraycopy(insertArr, 0, newArr, targetIndex + 1, insertArr.length);
// 復(fù)制目標(biāo)元素之后的元素
System.arraycopy(arr, targetIndex + 1, newArr,
targetIndex + 1 + insertArr.length,
arr.length - targetIndex - 1);
return newArr;
}
進(jìn)階操作三:刪除指定元素
代碼實現(xiàn)
/**
* 刪除數(shù)組中的指定元素
*
* @param arr 原數(shù)組
* @param target 要刪除的目標(biāo)元素
* @return 刪除后的新數(shù)組
*/
public static int[] deleteElement(int[] arr, int target) {
int targetIndex = findElement(arr, target);
// 未找到目標(biāo)元素,返回原數(shù)組副本
if (targetIndex == -1) {
return arr.clone();
}
// 創(chuàng)建新數(shù)組,長度-1
int[] newArr = new int[arr.length - 1];
// 復(fù)制目標(biāo)元素之前的元素
System.arraycopy(arr, 0, newArr, 0, targetIndex);
// 復(fù)制目標(biāo)元素之后的元素
System.arraycopy(arr, targetIndex + 1, newArr, targetIndex,
arr.length - targetIndex - 1);
return newArr;
}
總結(jié)
本文通過數(shù)組查找、插入、刪除三大操作的實現(xiàn),展示了 Java 數(shù)組操作的核心技巧:
- 查找是基礎(chǔ):所有定位操作都依賴于元素查找功能
- 增刪靠復(fù)制:數(shù)組長度固定,增刪操作本質(zhì)是創(chuàng)建新數(shù)組并分區(qū)域復(fù)制元素
- 分段處理是關(guān)鍵:無論是插入還是刪除,都需要將數(shù)組分為目標(biāo)位置前后兩部分分別處理
到此這篇關(guān)于Java數(shù)組進(jìn)階操作方法核心技巧的文章就介紹到這了,更多相關(guān)Java數(shù)組操作方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring3 MVC請求參數(shù)獲取的幾種方法小結(jié)
本篇文章主要介紹了Spring3 MVC請求參數(shù)獲取的幾種方法小結(jié),非常具有實用價值,需要的朋友可以參考下。2017-03-03
springboot schedule 解決定時任務(wù)不執(zhí)行的問題
這篇文章主要介紹了springboot schedule 解決定時任務(wù)不執(zhí)行的問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
使用webservice自定義注解處理參數(shù)加解密問題
這篇文章主要介紹了使用webservice自定義注解處理參數(shù)加解密問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Java 隊列實現(xiàn)原理及簡單實現(xiàn)代碼
這篇文章主要介紹了Java 隊列實現(xiàn)原理及簡單實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10
spring中向一個單例bean中注入非單例bean的方法詳解
Spring是先將Bean對象實例化之后,再設(shè)置對象屬性,所以會先調(diào)用他的無參構(gòu)造函數(shù)實例化,每個對象存在一個map中,當(dāng)遇到依賴,就去map中調(diào)用對應(yīng)的單例對象,這篇文章主要給大家介紹了關(guān)于spring中向一個單例bean中注入非單例bean的相關(guān)資料,需要的朋友可以參考下2021-07-07

