Common method 1 of Collection: collection

Source: Internet
Author: User

Common method 1 of Collection: collection

Import java. util. *; public class CollectionTest {public static void main (String [] args) {Collection C = new ArrayList (); // create an ArratList set C. add (1); // The set can only contain reference data. C. add (new Integer (100); Object o = new Object (); C. add (o); // storage reference type, which is a memory address: Customer cus = new Customer ("Zhang San", 24); C. add (cuz); System. out. println ("the number of elements in the set is:" + C. size () + ", whether the set is empty:" + C. isEmpty (); Object O [] = C. toArray (); // converts a set into an array for (int I = 0; I <O. length; I ++) {System. out. println (the "+ I +" element of the set is: "+ O [I]);} C. clear (); System. out. println ("the number of elements in the set is:" + C. size () + ", whether the set is empty:" + C. isEmpty () ;}} class Customer {String name; int age; Customer (String name, int age) {this. name = name; this. age = age;} public String toString () {return "name [" + this. name + "], age [" + this. age + "]";}}

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.