The methods in java collection _ collection are embodied through Arraylist, And the java collection arraylist

Source: Internet
Author: User

The methods in java collection _ collection are embodied through Arraylist, And the java collection arraylist

Import java. util .*;

/*
Collection defines the common functions of the Collection framework.
1. Add
Add (e );
AddAll (collection );

2. Delete
Remove (e );
RemoveAll (collection );
Clear ();

3. Judge.
Contains (e );
IsEmpty ();

4. Obtain
Iterator ();
Size ();

5. Obtain the intersection.
RetainAll ();

6. The set changes the array.
ToArray ();



1. The parameter type of the add method is Object. To receive any type of objects.

2. All objects stored in the collection are referenced by the object address)


What is an iterator?
It is actually the way to retrieve elements from a set.
Like a clip in a doll game machine.

The iterator is used to retrieve elements in the set.
So the iterator is described in the form of internal classes.
Obtain the object of the internal class through the iterator () method of the container.




*/
Class CollectionDemo
{
Public static void main (String [] args)
{

Method_get ();
}
Public static void method_get ()
{
ArrayList al = new ArrayList ();

// 1. Add an element.
Al. add ("java01"); // add (Object obj );
Al. add ("java02 ");
Al. add ("java03 ");
Al. add ("java04 ");

/*
Iterator it = al. iterator (); // gets the Iterator used to retrieve elements in the set.

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 Al = new ArrayList ();

Al2.add ("java03 ");
Al2.add ("java04 ");
Al2.add ("java05 ");
Al2.add ("java06 ");


// Al1.retainAll (Al); // deintersection. al1 retains only the same elements as al2.
Al1.removeAll (Al );

Sop ("al1:" + al1 );
Sop ("Al:" + Al );




}

Public static void base_method ()
{
// Create a collection container. Subclass of the Collection interface. ArrayList
ArrayList al = new ArrayList ();

// 1. Add an element.
Al. add ("java01"); // add (Object obj );
Al. add ("java02 ");
Al. add ("java03 ");
Al. add ("java04 ");

// Print the original set.
Sop ("original set:" + al );


// 3. delete an element.
// Al. remove ("java02 ");
// Al. clear (); // clear the set.


// 4. Determine the element.
Sop ("Does java03 exist:" + al. contains ("java03 "));
Sop ("is the set empty? "+ Al. isEmpty ());


// 2. Obtain the number. Set length.
Sop ("size:" + al. size ());

// Print the changed set.
Sop (al );

}

Public static void sop (Object obj)
{
System. out. println (obj );
}
}

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.