JAVA List problems when using Remove

Source: Internet
Author: User
Tags arrays int size


Recently, I encountered a strange problem in the project. The reference code is as follows:

[Java]
Public class ListTest {
Public static List listFactory (){
Return new ArrayList (Arrays. asList ("a", "B", "c", "d "));
}

Public static void main (String [] args ){
List testList = null;
String t;

// Try to remove the interval elements a and c from the set
TestList = listFactory ();
For (int I = 0; I <testList. size (); I ++ ){
T = testList. get (I );
If (t. equals ("a") | t. equals ("c ")){
TestList. remove (t );
}
}
System. out. println ("result after removing the elements a and c:" + testList );

// Try to remove the adjacent elements a and B from the set
TestList = listFactory ();
For (int I = 0; I <testList. size (); I ++ ){
T = testList. get (I );
If (t. equals ("a") | t. equals ("B ")){
TestList. remove (t );
}
}
System. out. println ("result after removing adjacent elements a and B:" + testList );
}
}
[/Java]

The running result is as follows:

[Java]
After removing the elements a and c, the result is [B, d].
After the adjacent elements a and B are removed, the result is: [B, c, d].
[/Java]

According to the running results, the remove method is used in the List operation to successfully remove the elements at the interval, which may cause missed deletion when the adjacent elements are removed.

Cause of failure
After viewing the source code of remove (), we can find that the following method is used to implement remove in List.

[Java]
Public boolean remove (Object o ){
If (o = null ){
For (int index = 0; index <size; index ++)
If (elementData [index] = null ){
FastRemove (index );
Return true;
}
} Else {
For (int index = 0; index <size; index ++)
If (o. equals (elementData [index]) {
FastRemove (index );
Return true;
}
}
Return false;
}

Private void fastRemove (int index ){
ModCount ++;
Int numMoved = size-index-1;
If (numMoved> 0)
System. arraycopy (elementData, index + 1, elementData, index,
NumMoved );
ElementData [-size] = null; // Let gc do its work
}
[/Java]

The fastRemove method is the key to implementation. From the implementation point of view, the elements after the elements to be deleted are moved one by one. When we delete a loop, if the current index of the outer layer is 1, delete it, move the subsequent elements forward, and then add 1 to the outer index to continue the loop, in this way, the next element of the deleted element will not be traversed, leading to missed deletion.

Solution
Methods for deleting objects in reverse order [java]
Public class ListTest {
Public static List listFactory (){
Return new ArrayList (Arrays. asList ("a", "B", "c", "d "));
}
Public static void main (String [] args ){
List testList = null;
String t;

// After the adjacent elements a and B are removed in reverse order
TestList = listFactory ();
Int size = testList. size ();
For (int I = size-1; I> = 0; I -){
T = testList. get (I );
If (t. equals ("a") | t. equals ("B ")){
TestList. remove (t );
}
}
System. out. println ("result after removing adjacent elements a and B in reverse order:" + testList );
}
}
[/Java]

Use iterator to delete [java]
Public class ListTest {
Public static List <String> listFactory (){
Return new ArrayList <String> (Arrays. asList ("a", "B", "c", "d "));
}
Public static void main (String [] args ){
List <String> testList = null;
String t;

// Use iterator to remove adjacent elements a and B
TestList = listFactory ();
Iterator <String> iter = testList. iterator ();
While (iter. hasNext ()){
T = iter. next ();
If (t. equals ("a") | t. equals ("B ")){
Iter. remove ();
}
}
System. out. println ("result after removing adjacent elements a and B using iterator:" + testList );
}
}
[/Java]

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.