Java: Container/collection ()

Source: Internet
Author: User

/**
* Collection Interface
* Common methods:
* Add (Object e) To ensure that this collection contains the specified element (optional operation).
* Size (): Gets the number of elements in the collection
* Remove (Object e): remove element
* Clear (): empties the elements in the collection
* CONTAINS (Object e): Determines whether the specified element is contained in the collection
* IsEmpty (): Determines whether the collection is empty
* Iterator (): Gets the iterator that corresponds to the collection.
*--list interface: Not unique, ordered (insert order)
*----ArrayList class: variable-length arrays, which are essentially implemented by arrays, where in-memory storage space is continuous
*------Advantages: Random access or high efficiency over time
*------Cons: Insertion and deletion require a lot of movement of the position of the element, which is less efficient.
* ArrayList common method of construction
* ArrayList () constructs an empty list with an initial capacity of 10.
* ArrayList (int initialcapacity) constructs an empty list with the specified initial capacity.
* Common methods:
* Add (Object E): Adds an element to the collection.
* Add (int index, E Element) inserts the specified element into the specified position in this list.
* Get (int index) returns the element at the specified position in this list. Subscript starting from 0
*--set interface: Unique, unordered
* Map interface: Store with key-value pairs.
*/

import Java.util.arraylist;import java.util.List; Public classTestarraylist { Public Static voidMain (string[] args) {//Collection list = new ArrayList ();List List =NewArrayList (); //adding elementsList.add ("AA"); List.add ("BB"); List.add ("cc"); List.add (2,"DD"); //Traverse Output         for(intI=0; I<list.size (); i++) {Stringstring= (String) list.Get(i); System. out. println (string); }    }}

*java.util.arraylist class
*add (Object obj): adding elements
*object get (int index): Gets the element that specifies the position of the subscript.
* Note: When an element is called by the Add method, the element is transformed upward to the object type, all using the Get method to get the return value to object

import Java.util.arraylist;import java.util.List; Public classTestArrayList2 { Public Static voidMain (string[] args) {Student Student=NewStudent ("Zhangsan", -); Student Student2=NewStudent ("zhangsan2", A); Student Student3=NewStudent ("zhangsan3", -); List List=NewArrayList ();        List.add (student);        List.add (Student2);        List.add (STUDENT3); //Traverse Output         for(intI=0; I<list.size (); i++) {Student stu= (Student) list.Get(i);//Forced ConversionsSystem. out. println (Stu.getname () +"---"+stu.getage ()); } System. out. println ("-----------");  for(Object obj:list) {Student stu=(Student) obj; System. out. println (Stu.getname () +"---"+stu.getage ()); }     }}

Java: Container/collection ()

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.