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