一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java中刪除 數(shù)組中的指定元素方法

java中刪除 數(shù)組中的指定元素方法

2020-07-25 15:57jingxian Java教程

下面小編就為大家?guī)硪黄猨ava中刪除 數(shù)組中的指定元素方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

java刪除 數(shù)組中的指定元素要如何來實現(xiàn)呢,如果各位對于這個算法不是很清楚可以和小編一起來看一篇關于java中刪除 數(shù)組中的指定元素的例子。

java的api中,并沒有提供刪除數(shù)組中元素的方法。雖然數(shù)組是一個對象,不過并沒有提供add()、remove()或查找元素的方法。這就是為什么類似ArrayList和HashSet受歡迎的原因。

不過,我們要感謝Apache Commons Utils,我們可以使用這個庫的ArrayUtils類來輕易的刪除數(shù)組中的元素。不過有一點需要注意,數(shù)組是在大小是固定的,這意味這我們刪除元素后,并不會減少數(shù)組的大小。

所以,我們只能創(chuàng)建一個新的數(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

下載好后,導入jar。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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]

當然,我們還有其他的方法,不過使用已經(jīng)的庫或java api來實現(xiàn),更快速。

我們來看下ArrayUtils.remove(int[] array, int index)

方法源代碼:

?
1
2
3
public static int[] remove(int[] array, int index) {
   return (int[])((int[])remove((Object)array, index));
 }

在跳轉(zhuǎn)到remove((Object)array, index)) ,源代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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ù)組,然后利用System.arraycopy()方法,將除了要刪除的元素外的其他元素都拷貝到新的數(shù)組中,然后返回這個新的數(shù)組。

以上就是小編為大家?guī)淼膉ava中刪除 數(shù)組中的指定元素方法全部內(nèi)容了,希望大家多多支持服務器之家~

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品色拉拉免费看 | 香蕉tv国产在线永久播放 | 国内精品一区视频在线播放 | 成人猫咪maomiav永久网址 | 亚洲国产成人久久综合区 | 日本特黄一级大片 | 韩国一大片a毛片女同 | 12345国产精品高清在线 | 人与禽交3d动漫羞羞动漫 | 美国艳星lisann成人作品 | 无人知晓小说姜璟免费阅读 | 视频在线观看一区二区 | 青草视频在线观看免费资源 | 亚洲AV综合99一二三四区 | 冰雪奇缘1完整版免费观看 变形金刚第一部 | 情侣奴伺候女王第2部分小说 | xx欧美老妇| 亚洲一卡2卡三卡4卡5卡组 | 亚洲免费二区 | x8x8在线永久免费观看 | 亚洲精品国产一区二区三区在 | 嫩草成人国产精品 | 大象视频污 | 99re这里只有精品视频 | 亚洲国产精品热久久 | 香港日本三级亚洲三级 | 视频在线观看高清免费 | 亚洲欧美国产自拍 | 猫咪社区免费资源在线观看 | 亚洲男人天堂a | www.毛片网站 | 四色6677最新永久网站 | 精品成人一区二区 | 大学第一次基本都没了 | 国产精品成人免费 | 亚洲国产日韩欧美一区二区三区 | 大桥未久aⅴ一区二区 | 国产在线步兵一区二区三区 | 亚州精品视频 | 欧美一区高清 | 国产精品区牛牛影院 |