List how remove is particularly error prone

Source: Internet
Author: User

Java in the operation of the list is very frequent, especially for the list start traversal, these operations we will, but also very familiar with, but the Java list to delete elements, remove list elements are not familiar with it, can be said to be unfamiliar, is the actual operation is also prone to error, Let's take a look at how to remove elements from the list in Java below.

    1. public class Test {
    2. public static void Main (string[] args) {
    3. String str1 = new String ("ABCDE");
    4. String str2 = new String ("ABCDE");
    5. String str3 = new String ("ABCDE");
    6. String STR4 = new String ("ABCDE");
    7. String STR5 = new String ("ABCDE");
    8. List List = new ArrayList ();
    9. List.add (STR1);
    10. List.add (STR2);
    11. List.add (STR3);
    12. List.add (STR4);
    13. List.add (STR5);
    14. System.out.println ("list.size () =" + list.size ());
    15. for (int i = 0; i < list.size (); i++) {
    16. if ((String) list.get (i)). StartsWith ("ABCDE")) {
    17. List.remove (i);
    18. }
    19. }
    20. System.out.println ("after remove:list.size () =" + list.size ());
    21. }
    22. }

What do you think the result of this program is printed out?

Java code
    1. The result of the operation is not:
    2. List.size () =5
    3. After Remove:list.size () =0

But:

Java code
    1. List.size () =5
    2. After Remove:list.size () =2

What's going on here? How do you want to remove the elements from the list?

Cause: After the list removes an element each time, the subsequent elements move forward, and if I=i+1 is executed, the element that has just been moved is not read.

How to solve? There are three ways to solve this problem:

1. Traverse the list backwards

Java code
    1. for (int i = List.size ()-1; i > =0; i--) {
    2. if ((String) list.get (i)). StartsWith ("ABCDE")) {
    3. List.remove (i);
    4. }
    5. }

2. Move the I back after each element is removed

Java Code
    1. for (int i = 0; i < list.size (); i++) {
    2. if ((String) list.get (i)). StartsWith ("ABCDE")) {
    3. List.remove (i);
    4. I=i-1;
    5. }
    6. }

3. Use the Iterator.remove () method to remove

Java Code
      1. for (Iterator it = List.iterator (); It.hasnext ();) {
      2. String str = (string) it.next ();
      3. if (Str.equals ("Chengang")) {
      4. It.remove ();
      5. }
      6. }
      7. Http://blog.sina.com.cn/s/blog_621b6f0e0100s5n5.html

List how remove is particularly error prone

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.