Java ArrayList class Collection

Source: Internet
Author: User
Tags array length

Collection Framework (collection Origin and collection inheritance system diagram)


* A: The origin of the collection
* The array length is fixed, when the added element exceeds the length of the array needs to be redefined, too cumbersome, Java provides us with a collection class, can store arbitrary objects, the length can be changed, as the element increases, and decreases as the element decreases.


* B: The difference between an array and a set


* Difference 1:
* Arrays can store both the base data type and the reference data type, the base data type stores the value, and the reference data type stores the address value
* The collection can store only reference data types (objects) in the collection, and the base data types are stored, but are automatically boxed into objects when stored


* Difference 2:
* The array length is fixed and does not grow automatically
* The length of the set is variable, and can grow according to the increment of the element


* C: Arrays and collections when to use
* 1, if the number of elements is a fixed recommended array
* 2, if the number of elements is not a fixed recommended set


* D: Set inheritance system diagram

Collection Framework (Basic functional testing of the collection collection)
* A: Case Presentation
*
Basic function Demo

Boolean Add (E E)
Boolean remove (Object o)
void Clear ()
Boolean contains (Object o)
Boolean IsEmpty ()
int size ()



Collection Frame (collection traversal collection to array traversal)
* A: Traversal of the collection
* In fact, you get each element in the collection in turn.
* B: Case Demo
* The collection can be traversed by transferring the set to the array.
* ToArray ()
*

Collection coll = new ArrayList ();
Coll.add (New Student ("Zhang San"),//object obj = new Student ("Zhang San", 23);
Coll.add (New Student ("John Doe", 24));
Coll.add (New Student ("Harry", 25));
Coll.add (New Student ("Zhao Liu", 26));

object[] arr = Coll.toarray ();//convert collection to array
for (int i = 0; i < arr.length; i++) {
Student s = (Student) arr[i]; //strong turn into student to access student-specific method down transformation
System.out.println (S.getname () + "," + s.getage ());
}

Collection frame (with all function test for collection collection)
* A: Case Presentation
*
Feature demo with all


There are two collections that operate two sets of
Boolean AddAll (Collection C)//Add collection A to collection B
Boolean RemoveAll (Collection c)
Boolean containsall (Collection c) //Determines whether the called collection contains an incoming collection
Boolean retainall (Collection C)//Fetch intersection

Collection C1 = new ArrayList ();
C1.add ("a");
C1.add ("B");
C1.add ("C");
C1.add ("D");

Collection C2 = new ArrayList ();
C2.add ("a");
C2.add ("B");
C2.add ("C");
C2.add ("D");
C2.add ("E");
C2.add ("F");

Returns True if the set of calls is changed, False if the set of calls is not changed
Boolean B = C1.retainall (C2);//Take Intersection
System.out.println (b);
SYSTEM.OUT.PRINTLN (C1);

Java ArrayList class 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.