How does ArrayList Delete specified data ?, Arraylist

Source: Internet
Author: User

How does ArrayList Delete specified data ?, Arraylist

Import java. util. ArrayList;


Public class tttt {
Public static void main (String [] args ){
ArrayList <Integer> arrayList = new ArrayList <Integer> ();
ArrayList <Integer> arrayList2 = new ArrayList <Integer> ();
For (int I = 0; I <10; I ++ ){
ArrayList. add (I );
ArrayList2.add (I );
}
 
 
 
For (int I = 0; I <arrayList. size (); I ++ ){
Integer integer = arrayList. get (I );
If (integer = 3 | integer = 4 | integer = 5 ){
ArrayList. remove (integer );
}
}
System. out. println (arrayList. toString ());

[0, 1, 2, 4, 6, 7, 8, 9] 4 has not been deleted yet. If so, delete it! Because when 3 is deleted, 4 runs to the original position of 3 and avoids traversal (3)

 

// Delete the data in this way. Put the data that meets the requirements in another set, and delete the data in a unified manner !!!!
ArrayList <Integer> arrayList3 = new ArrayList <Integer> ();
 
For (int I = 0; I <arrayList2.size (); I ++ ){
Integer integer = arrayList2.get (I );
If (integer = 3 | integer = 4 | integer = 5 ){
// ArrayList2.remove (integer );
ArrayList3.add (integer );
}

}
ArrayList2.removeAll (arrayList3 );
System. out. println (arrayList2.toString ());

[0, 1, 2, 6, 7, 8, 9] deleted

}
}

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.