JAVA- basis (collection)

JAVA- basis (collection Collection)

1. What is a collection?

Java is a collection container provided, it can be used to store a plurality of data. Somewhat similar to the array.

2. The difference between the set and the array?

Length of the array is fixed. The collection length is variable.

Stored in the array element of the same type, you can store basic data type value. Collection of objects are stored. And the type of the object can be inconsistent. When the object is generally more time, use the collection is stored in development.

3. What are the main collection?

Memory structure according to which a collection may be divided into two categories, namely, a set of single and dual column set, is set Collection single, double sets of columns to Map.

4. What is the single collection? What is a double set of columns?

Single set, somewhat similar array may simply be considered as a rope, double row name implies, two columns, but two columns there is a correspondence relationship, key and value.

What 5.Collection that?

Root interface separate set of classes, which are mainly sub-interfaces List and Set. The difference would write List and Set differences article. The collection itself is a tool, it is stored in the java.util package. In Collectionthe interface definition of the most common single set of frame content.

6.Collection way?

public boolean add(E e): The given object to the current collection.

public void clear() : Empty the collection of all the elements.

public boolean remove(E e): Delete the given object in the current collection.

public boolean contains(E e): Analyzing current collection contains the given object.

public boolean isEmpty(): To determine whether the current collection is empty.

public int size(): Returns the number of elements in a set.

public Object[] toArray(): The set of elements stored in the array.

7. traversal Collection?

(1.) Iterator traversal.

  public E next(): Return the next element iteration.

  public boolean hasNext(): If the iteration has more elements, it returns true. 

. 1  public  class IteratorDemo {
 2        public  static  void main (String [] args) {
 . 3          // create an object with a polymorphic manner 
. 4          Collection <String> = Coll new new the ArrayList <String> ();
 . 5  
. 6          // add elements to the set 
7          coll.add ( "string of star people" );
 8          coll.add ( "Tucao star people" );
 9          coll.add ( "Wang Star people" );
 10          // traverse
 11          // use iterates over each object has a collection of own iterator 
12 is          the iterator <String> IT = coll.iterator ();
 13 is         //   generic data type refers to an iterative element 
14          the while (it.hasNext ()) { // determines whether iterative element 
15              String it.next S = (); // Get an iterative element 
16              the System. Out.println (S);
 . 17          }
 18 is        }
 . 19 }

(2) enhanced for loop

for (variable data element type: Collection set or array) { 
 // Write operation code 
}

It is used to traverse the Collection and arrays. Usually carried out only through the elements, additions and deletions do not operate on a set of elements in the process of traversal.

(3) ordinary for loop

  This way is not very good, I do not recommend we do not write, has little significance.

 

Guess you like

Origin www.cnblogs.com/fan123yh/p/10981351.html