1. Basic functional testing of the collection collection:
1 Packagecn.itcast_01;2 3 Importjava.util.ArrayList;4 Importjava.util.Collection;5 6 /*7 * The origin of the collection:8 * We are learning object-oriented language, and object-oriented language description of things is through the object, in order to facilitate the operation of multiple objects, we have to store these multiple objects. 9 * If you want to store multiple objects, you can't be a basic variable, but a variable of a container type, what are the container types in the knowledge we've learned so far?Ten * Arrays and StringBuffer. But what? The result of StringBuffer is a string that does not necessarily meet our requirements, so we can only select an array, which is an array of objects. One * and the object array does not adapt to the needs of change, because the length of the array is fixed, this time, in order to adapt to the needs of change, Java provides a collection of classes for us to use. A * - * What are the differences between arrays and collections? - * A: Length difference the * Fixed length of array - * Set length variable - * B: Different content - * Arrays are stored with elements of the same type + * While collections can store different types of elements - * C: Data type issues for elements + * Arrays can store basic data types, or they can store reference data types A * Collection can only store reference types at * - * Just said that the collection is stored in multiple elements, but we also have different requirements for storing multiple components: for example, I want to not have the same elements in these multiple elements, - * Again, for example, I want these elements to be sorted by some sort of rule. Java provides a variety of collection classes for different requirements, so Java provides a number of collection classes. - * The data structure of these multiple collection classes is different, the structure is not important, it is important to be able to store things, but also to be able to use these things, such as judgment, access and so on. - * In this case, then, these multiple collection classes are common content, we put the common content of these collection classes upward extraction, and finally can form a set of inheritance architecture. - * in * Data structure: How it is stored. - * to * Collection: Is the top-level interface of the set, its sub-system has the repetition, has the unique, has the orderly, has the disorder. (It will be explained slowly later.) + * - * Overview of collection features: the * 1: Add features * * Boolean Add (Object obj): Add an element $ * Boolean addall (Collection C): Add an element of a collectionPanax Notoginseng * 2: Delete function - * void Clear (): Remove all elements the * Boolean remove (Object o): Removes an element + * Boolean removeall (Collection c): Removes the element of a collection (is one or all) A * 3: Judging function the * Boolean contains (Object O): Determines whether the specified element is contained in the collection + * Boolean containsall (Collection C): Determines whether the collection contains the specified set element (is one or all) - * Boolean isEmpty (): Determines whether the collection is empty $ * 4: Get features $ * iterator<e> Iterator () (key) - * 5: Length function - * int size (): Number of elements the * Interview question: Does the array have the length () method? Does the string have the length () method? Does the collection have the length () method? - * 6: Intersection functionWuyi * Boolean retainall (Collection C): Two elements of a collection? Where is the thought element, and what does the Boolean return mean ? the * 7: Convert the set to an array - * object[] ToArray () Wu */ - Public classCollectiondemo { About Public Static voidMain (string[] args) { $ //to test a method without all - - //To create a collection object - //Collection C = new Collection ();//error because the interface cannot be instantiated ACollection C =NewArrayList (); + the //boolean Add (Object obj): Add an element - //System.out.println ("Add:" +c.add ("Hello")); $C.add ("Hello"); theC.add ("World"); theC.add ("Java"); the the //void Clear (): Remove all elements - //c.clear (); in the //boolean remove (Object o): Removes an element the //System.out.println ("Remove:" + c.remove ("Hello")); //true About //System.out.println ("Remove:" + c.remove ("java ee")); //false the the //Boolean contains (Object O): Determines whether the specified element is contained in the collection the //System.out.println ("contains:" +c.contains ("Hello")); + //System.out.println ("contains:" +c.contains ("Android")); - the //boolean isEmpty (): Determines whether the collection is emptyBayi //System.out.println ("IsEmpty:" +c.isempty ()); the the //int size (): Number of elements -SYSTEM.OUT.PRINTLN ("Size:" +c.size ()); - theSystem.out.println ("C:" +c); the } the}
Java Basic Knowledge Hardening Collection Framework Note 04:collection basic function test of collection