Some minor issues about arraylist. remove.

Source: Internet
Author: User


ArrayList<Integer> arr = ArrayList<Integer>( i = 10;i<15;i++10 }
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 10, Size: 554738715)

An out-of-bounds exception is reported when the "10" element is removed from arr.

The reason is:

 

E remove (1 =? Get (I) =: The minimum index element of o. equals (get (I) (if such an element exists ). If the list contains the specified element, return (or equivalent to this case: if the list is changed due to a call ).

 

        ArrayList<Integer> arr =  ArrayList<Integer>( i = 10;i<15;i++        arr.remove((Integer)10);

 

 

        ArrayList<Integer> arr =  ArrayList<Integer>( i = 10;i<15;i++ arrSize =( i = 0;i<arrSize;i++(10 ==

The above Code cannot be executed, and an out-of-bounds exception is reported.

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 454732217)

 

ArrayList <Integer> arr = ArrayList <Integer> (I = 10; I <15; I ++ 1010101011 "before deletion:" + (I = 0; I <arr. size (); I ++ (10 = "after deletion:" + arr. size ());

 

Take a closer look at the execution results:

Before deletion: 107

What are the two conditions in the preceding case when arraylist is removed?

The reason is that the size after each remove operation changes, but the iteration base is not dynamically adjusted based on the size after the remove operation, resulting in incomplete cross-border and set traversal.

 

The correct method is as follows:

ArrayList <Integer> arr = ArrayList <Integer> (I = 10; I <15; I ++ 1010101011 "before deletion:" + (I = 0; I <arr. size (); I ++ (10 = -- "after deletion:" + arr. size ());

After the remove operation, the index location is-1,

Or use an iterator.

ArrayList <Integer> arr = ArrayList <Integer> (I = 10; I <15; I ++ 1010101011 "before deletion:" + <Integer> it = (10 =

 

 


 

 

 

 

 

 

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.