Import java.util.*;
/*
Collection defines the common functionality of the collection framework.
1, adding
Add (e);
AddAll (collection);
2, delete
Remove (e);
RemoveAll (collection);
Clear ();
3, Judge.
Contains (e);
IsEmpty ();
4, get
Iterator ();
Size ();
5, gets the intersection.
Retainall ();
6, set of set variables.
ToArray ();
The parameter type of the 1,add method is object. To make it easy to receive any type of object.
2, the object's reference (address) is stored in the collection
What is an iterator?
is actually the way the collection is taken out of the element.
Like a gripper in a doll game machine.
Iterators are fetch methods that directly access the elements in the collection.
So the iterator is described in the form of an inner class.
Gets the object of the inner class through the container's iterator () method.
*/
Class Collectiondemo
{
public static void Main (string[] args)
{
Method_get ();
}
public static void Method_get ()
{
ArrayList al = new ArrayList ();
1, add the element.
Al.add ("Java01");//add (Object obj);
Al.add ("Java02");
Al.add ("java03");
Al.add ("Java04");
/*
Iterator it = Al.iterator ();//Gets an iterator that is used to remove an element from the collection.
while (It.hasnext ())
{
SOP (It.next ());
}
*/
for (Iterator it = Al.iterator (); It.hasnext ();)
{
SOP (It.next ());
}
}
public static void Method_2 ()
{
ArrayList al1 = new ArrayList ();
Al1.add ("Java01");
Al1.add ("Java02");
Al1.add ("java03");
Al1.add ("Java04");
ArrayList Al2 = new ArrayList ();
Al2.add ("java03");
Al2.add ("Java04");
Al2.add ("java05");
Al2.add ("java06");
Al1.retainall (AL2);//To Intersect, AL1 only retains the same elements as Al2.
Al1.removeall (AL2);
SOP ("Al1:" +al1);
SOP ("Al2:" +al2);
}
public static void Base_method ()
{
Creates a collection container. Subclasses that use the collection interface. ArrayList
ArrayList al = new ArrayList ();
1, add the element.
Al.add ("Java01");//add (Object obj);
Al.add ("Java02");
Al.add ("java03");
Al.add ("Java04");
Prints the original collection.
SOP ("Original set:" +al);
3. Delete the element.
Al.remove ("Java02");
Al.clear ();//emptying the collection.
4, judging elements.
SOP ("JAVA03 is present:" +al.contains ("java03"));
SOP ("Is the collection empty? "+al.isempty ());
2, get the number. The collection length.
SOP ("Size:" +al.size ());
Prints the changed collection.
SOP (AL);
}
public static void Sop (Object obj)
{
System.out.println (obj);
}
}
The methods in the Java collection _collection are represented by allaylist