Static import, enhanced for loop

Source: Internet
Author: User
Tags int size iterable

One, static import: JDK1.5 when the appearance of
1, static import role: Simplified writing, static import can be used for a class of all static members.

2. Statically imported formats:
Import static package name. class name. static members;

Package mfq.jdk;
Import java.util.ArrayList;
Import java.util.Collections; /* public class Demo1 {public static void main (string[] args) {arraylist<integer> list = new ArrayList
        <Integer> ();
        List.add (16);
        List.add (12);
        List.add (18);

        List.add (15);
        Sort Collections.sort (list);
        System.out.println ("Elements of the Set" +list);
    System.out.println ("Index value" +collections.binarysearch (list, 12));
} */import static java.util.collections.*; 
Import static Java.util.Collections.sort;
Import static Java.util.Collections.binarySearch; 
Import static Java.util.Collections.max;
Import static java.lang.System.out; public class Demo1 {public static void main (string[] args) {arraylist<integer> list = new Arraylist&lt
        ;integer> ();
        List.add (16);
        List.add (12);
        List.add (18);

        List.add (15);
        Sort (list);
        System.out.println ("Elements of the Set" + list); System.oUt.println ("index value" + BinarySearch (list, 12));
        System.out.println ("Maximum:" + max (list));
        Out.println ("Elements of the Set" + list);
        Out.println ("index value" + BinarySearch (list, 12));
    Out.println ("Maximum:" + max (list)); }
}

3, static import should pay attention to the matters:
1 if a statically imported member has the same name as a member of this class, the static members of this class are used by default, and if you need to specify a member with a static import, you need to precede the static member with the class name.
2) give examples to illustrate:

Second, enhance for loop:
1. Enhance the purpose of the FOR loop: simplify the writing format of the iterator . (Note: The bottom of an enhanced for loop or iterator traversal is used.) )

2, enhance the scope of application for the loop: If you implement the Iterable interface of the object or an array object can use the enhanced for loop.

3, enhance the format of the For loop:

For (data type  variable name  : Traversal target) {

}

4, enhance for the loop to note the matters:
1) Enhanced for loop The bottom is also used by the iterator to get, but get the iterator is done by the JVM, do not need us to get the iterator, so in the use of the enhanced for loop variable elements in the process of using the collection object to modify the number of elements of the collection.

2 The difference between an iterator traversing an element and an enhanced for loop variable element: You can delete the elements of a collection when you use an iterator to traverse the elements of a collection, and when you enhance the elements of a For loop variable collection, you cannot invoke the Remove method of the iterator to delete the elements.

3 The difference between a general for loop and an enhanced for loop: The normal for loop can have no target for the variable, and an enhanced for loop must have a variable target.

5. Add:

6, the custom class uses the enhanced for loop

package mfq.jdk;

Import Java.util.Iterator;

    Customizing a class uses the enhanced for loop class MyList implements iterable<string>{object[] arr = new OBJECT[10];
    int index = 0;//current pointer to public void add (Object o) {arr[index++] = O;
    public int size (): {return index; Iterator<string> iterator () {return new iterator<string> () {int cursor =
            0;//Current Cursor public boolean hasnext () {return cursor<index;
            Public String Next () {return (String) arr[cursor++];
    The public void Remove () {}};
        } public class Demo6 {public static void main (string[] args) {MyList list = new MyList ();
        List.add ("Lucy");
        List.add ("Endeavor");

        List.add ("Lily");
        for (String item:list) {System.out.println ("element" +item); }
    }
}

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.