Java ArrayList Small Program understanding

Source: Internet
Author: User

Package Collection;import Java.util.arraylist;import Java.util.iterator;//import Javax.xml.crypto.AlgorithmMethod ;/* 1. The argument type of the Add method is object, so that you can receive object 2 of any type. The collection is stored in the object's reference (address) iterator: The way the collection takes out the elements */public class clloectiondemo{/** * @param args */public static void main (string[] args) {//TODO auto-generated method stub//method2 (); Method_get ();} public static void Method_get () {arraylist<string> al = new arraylist<string> (); Al.add ("java1"); Al.add (" Java2 "); Al.add (" java3 ");//SOP (AL);//iterator (): Returns the sub-object of the Iterator interface//interface reference can only point to the subclass object of its own interface, which is a method new from the collection object// Iterator it = Al.iterator ();/* Onewhile (It.hasnext ()) {SOP (It.next ());} *//* twofor (int i = 0; i < al.size (); i++) {SOP (It.next ());} *///threefor (Iterator ITT = Al.iterator (); Itt.hasnext ();) {SOP (Itt.next ());}} public static void Method2 () {arraylist<string> al = new arraylist<string> (); Al.add ("java1"); Al.add ("Java2 "); Al.add (" java3 "); arraylist<string> A2 = new arraylist<string> () A2.add ("JAVA1"); A2.add ("Java5 "); A2.add (" Java6 ");//Take the intersection, take the same element//al.retainall (A2); Al.removeall (A2); SOP ("Al:" +al); SOP ("A2:" +A2);} public static void Base_method () {//Creates a collection container that uses subclasses of the collection interface. Arraylistarraylist<string> al = new arraylist<string> ();//Add Al.add ("heh"); Al.add ("DHF"); Al.add ("Fdjkgd    ");//Print SOP (" Original set: "+al); Delete Al.remove ("heh"); Sop (AL);//Empty set al.clear ();//IS NULL SOP ("is Empty:" +al.isempty ());//Judgment Element sop ("Heh exists:" +al.contains ("heh")); /Get Set length SOP ("Size:" +al.size ());} public static void Sop (Object obj) {System.out.println (obj);}}

  

Java ArrayList Small Program understanding

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.