/**
* 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 ()