java中刪除 數(shù)組中的指定元素方法
java中刪除 數(shù)組中的指定元素要如何來實(shí)現(xiàn)呢,如果各位對于這個(gè)算法不是很清楚可以和小編一起來看一篇關(guān)于java中刪除 數(shù)組中的指定元素的例子。
java的api中,并沒有提供刪除數(shù)組中元素的方法。雖然數(shù)組是一個(gè)對象,不過并沒有提供add()、remove()或查找元素的方法。這就是為什么類似ArrayList和HashSet受歡迎的原因。
不過,我們要感謝Apache Commons Utils,我們可以使用這個(gè)庫的ArrayUtils類來輕易的刪除數(shù)組中的元素。不過有一點(diǎn)需要注意,數(shù)組是在大小是固定的,這意味這我們刪除元素后,并不會(huì)減少數(shù)組的大小。
所以,我們只能創(chuàng)建一個(gè)新的數(shù)組,然后使用System.arrayCopy()方法將剩下的元素拷貝到新的數(shù)組中。對于對象數(shù)組,我們還可以將數(shù)組轉(zhuǎn)化為List,然后使用List提供的方法來刪除對象,然后再將List轉(zhuǎn)換為數(shù)組。
為了避免麻煩,我們使用第二種方法:
我們使用Apache commons庫中的ArrayUtils類根據(jù)索引來刪除我們指定的元素。
Apache commons lang3下載地址:
http://commons.apache.org/proper/commons-lang/download_lang.cgi
下載好后,導(dǎo)入jar。
import java.util.Arrays; import org.apache.commons.lang3.ArrayUtils; /** * * Java program to show how to remove element from Array in Java * This program shows How to use Apache Commons ArrayUtils to delete * elements from primitive array. * */ public class RemoveObjectFromArray{ public static void main(String args[]) { //let's create an array for demonstration purpose int[] test = new int[] { 101, 102, 103, 104, 105}; System.out.println("Original Array : size : " test.length ); System.out.println("Contents : " Arrays.toString(test)); //let's remove or delete an element from Array using Apache Commons ArrayUtils test = ArrayUtils.remove(test, 2); //removing element at index 2 //Size of array must be 1 less than original array after deleting an element System.out.println("Size of array after removing an element : " test.length); System.out.println("Content of Array after removing an object : " Arrays.toString(test)); } } Output: Original Array : size : 5 Contents : [101, 102, 103, 104, 105] Size of array after removing an element : 4 Content of Array after removing an object : [101, 102, 104, 105]
當(dāng)然,我們還有其他的方法,不過使用已經(jīng)的庫或java api來實(shí)現(xiàn),更快速。
我們來看下ArrayUtils.remove(int[] array, int index)
方法源代碼:
public static int[] remove(int[] array, int index) { return (int[])((int[])remove((Object)array, index)); }
在跳轉(zhuǎn)到remove((Object)array, index)) ,源代碼:
private static Object remove(Object array, int index) { int length = getLength(array); if(index >= 0 && index < length) { Object result = Array.newInstance(array.getClass().getComponentType(), length - 1); System.arraycopy(array, 0, result, 0, index); if(index < length - 1) { System.arraycopy(array, index 1, result, index, length - index - 1); } return result; } else { throw new IndexOutOfBoundsException("Index: " index ", Length: " length); } }
這下明白了ArrayUtils的刪除數(shù)組中元素的原理了吧。其實(shí)還是要用到兩個(gè)數(shù)組,然后利用System.arraycopy()方法,將除了要?jiǎng)h除的元素外的其他元素都拷貝到新的數(shù)組中,然后返回這個(gè)新的數(shù)組。
以上就是小編為大家?guī)淼膉ava中刪除 數(shù)組中的指定元素方法全部內(nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
詳解Spring Boot 部署jar和war的區(qū)別
本篇文章主要介紹了詳解Spring Boot 部署jar和war的區(qū)別,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09Java實(shí)現(xiàn)導(dǎo)出word表格的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言導(dǎo)出word表格功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的小伙伴可以參考一下2022-12-12spring AOP的Around增強(qiáng)實(shí)現(xiàn)方法分析
這篇文章主要介紹了spring AOP的Around增強(qiáng)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了spring面向切面AOP的Around增強(qiáng)具體步驟與相關(guān)操作方法,需要的朋友可以參考下2020-01-01Java數(shù)據(jù)結(jié)構(gòu)之順序表的實(shí)現(xiàn)
線性表(linear?list)是n個(gè)具有相同特性的數(shù)據(jù)元素的有限序列。順序表是常見的線性表之一,本文將詳細(xì)講講順序表的原理與實(shí)現(xiàn),需要的可以參考一下2022-08-08Mybatis實(shí)體類屬性與數(shù)據(jù)庫不一致解決方案
這篇文章主要介紹了Mybatis實(shí)體類屬性與數(shù)據(jù)庫不一致解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10java實(shí)現(xiàn)將數(shù)字轉(zhuǎn)換成人民幣大寫
前面給大家介紹過使用javascript,php,c#,python等語言實(shí)現(xiàn)人民幣大寫格式化,這篇文章主要介紹了java實(shí)現(xiàn)將數(shù)字轉(zhuǎn)換成人民幣大寫的代碼,非常的簡單實(shí)用,分享給大家,需要的朋友可以參考下2015-04-04