Deletes the specified element from the array in java.

Source: Internet
Author: User
Tags arrays

Java APIs do not provide methods to delete elements in arrays. Although an array is an object, the add (), remove (), or element search methods are not provided. This is why ArrayList and HashSet are popular. However, we would like to thank Apache Commons Utils for using the ArrayUtils class of this library to easily delete elements in the array. However, it should be noted that the array size is fixed, which means that the array size will not be reduced after the elements are deleted. Therefore, we can only create a new array, and then use the System. arrayCopy () method to copy the remaining elements to the new array. For the object array, we can also convert the array to List, then use the method provided by List to delete the object, and then convert the List to an array.
To avoid the trouble, we use the second method:

We use the ArrayUtils class in the Apache commons Library to delete the specified elements based on the index.

Apache commons lang3 download address:
Http://commons.apache.org/proper/commons-lang/download_lang.cgi
After downloading, import the jar file.


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 [] Testing = 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]

 
Of course, we still have other methods, but we can use the library or java api to implement it more quickly.
Let's take a look at the source code of the ArrayUtils. remove (int [] array, int index) method:

Public static int [] remove (int [] array, int index ){
Return (int []) (int []) remove (Object) array, index ));
    }

 
In the jump to remove (Object) array, index), source code:

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 );
        }
    }

 
Now I understand how ArrayUtils deletes elements in an array. In fact, we still need to use two arrays, and then use System. the arraycopy () method copies all elements except the elements to be deleted to the new array and returns the new array.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.