Android Training (Java article) Nineth Day-Container

Source: Internet
Author: User

  1. The container is also called the collection, can store different types of elements, and the space is multiplied;

    650) this.width=650; "Src=" http://note.youdao.com/yws/public/resource/719ed66494263eb983dad4d9b8549a6b/ Eed19747f3d849fd902f5d0453fd229e/01cc7576f44c466f920082e543178f19 "style=" Border:none;height:auto; "alt=" 01cc7576f44c466f920082e543178f19 "/>

    Distinction: Collection has no inheritance relationship with map;

    Collection Common methods:

    Size (): The capacity of the container;

    Clear (): Clears all elements in the container;

    Add (Object O): Adds any type of element;

    Remove (Object o): Removes the specified element;

    RemoveAll (Collection C): Removes the specified container;

    Contains (Object O): Determines whether a given element is contained in a container;


  2. Set interface: Unordered and non-repeatable;

    What is disorder? What is the difference between disorder and randomness?

    A: The first execution of the code, the output is unordered, and once executed once, the output order will not change, unless the program is added and deleted operations;

    Disorder is limited to the first execution, random execution is not the same;

    Implementation class: HashSet, TreeSet;

    Common construction Methods: New HashSet (); New HashSet (Collection c);

    There is no extra method for set, so all of its methods are derived from the method inside the collection;


  3. List interface: Ordered can be repeated

    Implementation class: ArrayList, LinkedList;

    Common methods:

    Add (Object c);

    Add (int index,object o);//Insert an element o at the specified index;

    Remove (Object o);

    Remove (int index);//If the container has an integer element, index takes precedence as the subscript index;

    Get (int index);//Return object type

    Set (int index,object o);//replaces the specified index with the specified element o the element at index;

    Size ();//container capacity;

    Clear ();//Purge

    IndexOf (Object o);//Returns the index value of the first occurrence of the specified element o in the container;

    LastIndexOf (Object o);//Returns the index value of the last occurrence of the specified element o in the container;


  4. Packaging and Unpacking:

    Packaging: Converting the underlying type to an object; integer i=new integer (1);

    Unpacking: Converting an object into a basic type; int a=new Integer (1);

  5. Iterator:

    Iterator: Iterates over an array or container, but unlike a loop, an iteration is traversed only once, and if there is a stop in the middle of the traversal, then the traversal proceeds from where it was last stopped, rather than starting from the beginning of the loop;

    The collection interface has the iterator () method by default;

    Common methods:

    Hasnext ();//Determine whether there are elements on the right side of the cursor;

    Next ();//Returns the object type, given the specific element value of the container, and continues to judge the next time;

    Remove ();


  6. Enhanced for Loop foreach:

    Syntax: for (type variable name: Array or container name) {< output variable name can traverse;}

    Pros: Traverse extremely fast

    Disadvantage: There is no subscript, can not be used to sort, except traversal, no other method is provided;


  7. Generic type:

    Why is there a generic type?

    A: Let the specified type become unique, can improve the efficiency of the Code;

    Syntax: Class name < app Type > object name =new class name < reference type > ();


  8. Equals () and Hashcode () methods:

    Note: When an object exists, remember to rewrite equals () and hashcode ();

    Equals (): is only responsible for comparing the values of two objects for equality;

    Hashcode (): Determines whether two objects point to the same object (reference) and returns a hash code value;


  9. Map Interface:<k,v>;

    Key cannot be duplicated;

    Implementation class: HashMap, TreeMap, Linkedhashmap;

    Common methods:

    Put (Object K,object v);

    Get (Object key);//Returns the value mapped by key

    ContainsKey (Object o);//Determines whether the given element o is contained in the container, and if so, returns true;

    Containvalue (Object o);//Determines whether the value in the container contains the given element O, and returns true if yes;

    Remove (Object o);//delete the given element;


After-school exercises:

    1. Design a program to achieve 3 Chinese translation of English words based on generic map;

Code:

public class fanyi{

public static void Main (string[] args) {

HashMap m = new HashMap ();

M.put ("Apple", "Apple");

M.put ("pear", "pear");

M.put ("Orange", "orange");

For (String Str:m.keyset ()) {

System.out.println (str+ "English translation:" +m.get (str));

}

}

}











This article is from the "Kun" blog, please be sure to keep this source http://linyingkun.blog.51cto.com/2393912/1575543

Android Training (Java article) Nineth Day-Container

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.