5. Array and container--collection

Source: Internet
Author: User
Tags array length set set

An array: A collection of data of the same type, in fact he is also a container

1, the advantages of the array: You can automatically number the elements in the array starting from 0, easy to manipulate the data

2, the definition of the array:

Common in Java:

Format 1: Type [] Array name = new Type [number of array length or array element]; For example: int [] Dada = new Int[3];

Format 2: Type [] Array name = new Type []{value 1, value 2, value 3 ...}; For example: int [] data = new int{1,2,3,......};

It is exactly the same as the above meaning, more in line with the programming habits of programmers who have used C language programming:

Format 1: Type array name [] = new Type [number of array length or array element]; For example: int [] Dada = new Int[3];

Format 2: Type array name [] = new Type []{value 1, value 2, value 3 ...}; For example: int [] data = new int{1,2,3,......};

3. How the array is traversed: Gets the number of elements of the array through the property length.

How to use: array name. length

For example:

              Public Static voidMain (string[] args) {intData[] =New int[3];  for(inti=0;i<data.length;i++) Data[i]=i;  for(inti=0;i<data.length;i++) System.out.println ("The" +i+ "number for the array data is" +Data[i]); The result of the output is: the No. 0 digit of the array data is 0, the 1th digitof data is 1, the 2nd digit of data is 2 

Second, Collection: Divided into List, Set, map three kinds.

1, List: elements are ordered, elements can be repeated, because the aggregate has an index

A),ArrayList: Data structures used by the underlying data structure. Features are: Fast query speed, deletion slow, the thread is out of sync.

b),LinkedList: The linked list data structure used at the bottom. Features query speed is slow, adding and deleting speed, thread out of sync.

c),Vector: Underlying data structure, thread synchronization.

2, Set: Elements are unordered, elements can not be repeated.

A),HashSet: D

b),TreeSet: The elements in the set set can be sorted and the threads are out of sync.

3. MAP: Store key value pairs

A),HashMap: The underlying is a hash table data structure that can be stored as null as a key or value, the thread is not synchronized

b),HashTable: The underlying is a hash table data structure, can not be stored as null as a key or value, thread synchronization.

c),TreeMap: The bottom is a two-fork tree structure, the thread is out of sync.

Generic type: Used to solve security problems, is a type of security mechanism.

Generics are common in the collection framework, as long as you see <> define Generics

When using a collection, pass the data type you want to store in the collection as a parameter to <>

Traversal: That is, all elements in the socialize collection.

1. take the size () and get () method . Because the list is ordered, it can be traversed with this sort of method.

@Test  Public void Test () {    Listnew arraylist<string>();    List.add ("ddddd");    List.add ("eeeee");    List.add ("FFFFF");    print (list);     // because the list is in order, use the size () and get () methods to get     for (int i = 0; i < list.size (); i++) {      System.out.println (List.get (i));    

2. Traverse with iterator iterator

@Test  Public void Test () {     new  LinkedList ();     List.add ("123");     List.add ("456");     Iterator<String> it = list.iterator ();       while (It.hasnext ()) {       System.out.println (It.next ());}    }

3. Enhanced for Loop

@Test  Public void Test () {     new  LinkedList ();     List.add ("123");     List.add ("456");       for (List A:  list)       System.out.println (a);      }

4. How to traverse the map

@Test Public voidTest () {Map<String,String> map =NewTreemap<string, string>(); Map.put ("Jerry", "10000"); Map.put ("Shellway", "20000"); Map.put ("Kizi", "30000");    Print (Map.entryset ()); //Map's first traversal: Get key first, then get valueSet<string> sett =Map.keyset ();  for(String S:sett) {System.out.println (s+":"+Map.get (s)); }    //the second way to traverse map: To get a key value pair     for(Map.entry<string, string>Entry:map.entrySet ()) {System.out.println (Entry.getkey ()+" : "+Entry.getvalue ()); }}

V. The difference between a set and an array:

1, the length of the array is constant, the set length is variable.

2. The array stores the same type object, and the collection can store different types of objects.

            

5. Array and 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.