LIST _ delete an element

Source: Internet
Author: User

The project encountered the problem described in the question, but it was a bit difficult...

To sum up:

Here is a test Applet:

List <integer> ints = new arraylist <integer> ();
For (INT I = 0; I <100; I ++ ){
Ints. Add (I );
}
We delete all objects that cannot be divisible by 3...

First, the easiest thing to think of is the foreach loop, which determines whether to delete...

For (integer INTEGER: ints ){
If (integer % 3! = 0 ){
Ints. Remove (integer );
}
}
Run the command. An exception occurs:

Java. util. concurrentmodificationexception

I will not describe it in detail here. The page I will refer to will be provided at the end.

To put it simply, foreach is actually implemented by iterator, and iterator cannot be deleted when the set is used. Therefore, the preceding exception is thrown.

Then write the traditional for loop:

Int length = ints. Size ();
For (INT I = 0; I <length; I ++ ){
If (ints. Get (I) % 3! = 0 ){
Ints. Remove (I );
}
}
Run it. Exception: Java. Lang. indexoutofboundsexception

The reason is that with the deletion of the element, the size of the list changes, but the index does not change, it will cause the index> = size, and when it comes to the end, it will cross the border...

The problem is that I used to calculate the length before starting the loop, because each computing size has a performance loss...

Okay, so we have to calculate the size every time:

For (INT I = 0; I <ints. Size (); I ++ ){
If (ints. Get (I) % 3! = 0 ){
Ints. Remove (I );
}
}
Run it. You can run it, but the result is incorrect...

0
2
3
5
6
8

The reason is that the index does not match after deletion...

Now you can use the following tricks:

For (INT I = 0; I <ints. Size (); I ++ ){
If (ints. Get (I) % 3! = 0 ){
Ints. Remove (I );
I --;
}
}
After each condition is deleted, we delete the index to ensure normal operation...

However, this is definitely not a good solution...

In fact, iterator also provides the remove method, using this:

The premise is that no other thread is operating on this list, that is, if multiple operators operate on this list at the same time, the result will not be correct.

Iterator <integer> iterator = ints. iterator ();
While (iterator. hasnext ()){
Integer temp = iterator. Next ();
If (TEMP % 3! = 0 ){
Iterator. Remove ();
}
}

If you delete three objects, indexoutofboundsexception does not occur, but the deleted list is not the expected result. Specifically
Private void unsafedeletopbycount (INT count ){
Try {
For (INT I = 0; I <count; I ++ ){
List. Remove (I );
}
} Catch (exception e ){
E. printstacktrace ();
} Finally {
Print ();
}
}

Private void print (){
For (string STR: List ){
System. Out. println (STR );
}
}

List <string> List = new arraylist <string> ();
For (INT I = 0; I <6; I ++ ){
List. Add ("str" + I );
}
Unsafedeletopbycount (3 );
The output is as follows:
Str1
Str3
Str5
Why? After we delete the [str0] element with index 0, the element with index 0 will change to str1 due to the size change of the list, the element with index 1 Changes to str2.

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.