iterator&map&list&linkedhashmap& normal for loop & enhanced for Loop &bean

Source: Internet
Author: User
Tags comparable

Bo Master old Long wanted to summarize, but because no time, feel just to find excuses for themselves, but now spent a morning, finally tidy up, and comments more detailed, nonsense not much said, now on the code:

First, the blogger customizes a Bean class, in order to test the custom list

The bean inside the owner of the commonly used types of basic use, but do not recommend that you define the time to use float, really very troublesome

The other is get/set this piece, note that the Boolean type is the IS and set, not get, this everyone notice

Package cn.edu.sjzc.weiaccept.bean;/** * Created by Fanyafeng on 2015/5/8/0008.    */public class Fanyafengbean implements comparable{private int age;    Private long Height;    private float Phone;    private double hair;    Private String Name;    Private Boolean Sex;        Public Fanyafengbean (int-age, long-height, float phone, double hair, String name, Boolean sex) {age = age;        Height = height;        Phone = phone;        This.hair = hair;        name = name;    sex = sex;    } public int Getage () {return age;    public void Setage (int.) {age = age;    } public long GetHeight () {return Height;    public void SetHeight (long height) {height = height;    } public float Getphone () {return Phone;    } public void Setphone (float phone) {phone = phone;    } public double Gethair () {return hair;    } public void Sethair (double hair) {This.hair = hair; } public String GetnamE () {return Name;    } public void SetName (String name) {name = name;    } public boolean Issex () {return Sex;    public void Setsex (Boolean sex) {sex = sex;                } @Override Public String toString () {return ' fanyafengbean{' + ' age= ' + Age + ", height=" + Height + ", phone=" + Phone + ", hair=" + Hair + ", name= '" +    Name + ' \ ' + ', sex= ' + Sex + '} ';    } @Override public int compareTo (Object another) {return 0; }}
This is a blogger-defined Bean class, because it involves the order, so to implement a java.lang in the comparable, see the details

http://blog.csdn.net/qq_23195583/article/details/44174181

Then there is the specific method of writing:

Look at the first one:

Simple int,string.

private static void Intorstring () {/** * Briefly lists the traversal of a one-dimensional array * int,string, one is a normal for loop, and the other is an enhanced        For loop */int int_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};        for (int arrayitem:int_array) {System.out.print (Arrayitem + ",");        } System.out.println ();        System.out.println ("-------------------------------------------------------------------");        for (int i = 0; i < int_array.length; i++) {System.out.print (Int_array[i] + ",");        } System.out.println ();        System.out.println ("-------------------------------------------------------------------");        String string_array[] = {"Shian wind", "Software Engineering", "Java", "Android", "IOS"};        for (String Arrayitem:string_array) {System.out.print (Arrayitem + ",");        } System.out.println ();        System.out.println ("-------------------------------------------------------------------"); for (int i = 0; i < string_aRray.length;        i++) {System.out.print (String_array[i] + ",");        } System.out.println ();    System.out.println ("-------------------------------------------------------------------"); }
The second one:

There's more bread in here.

There's a note inside.

private static void Growing () {/** * Customizes a class that contains the commonly used type */int age = 0;        Long Height = 180;        float Phone = 15530128888f;        Double hair = 3000d;        String Name = "Shian Wind";        Boolean Sex = true;        String sex;        list<map<string, object>> fanyafenglist = new arraylist<map<string, object>> ();        list<fanyafengbean> Fanyafengbeans = new arraylist<fanyafengbean> (); fanyafengbean[] Fanyafengbeanarray = new fanyafengbean[]{new Fanyafengbean (age, Height, 18032278888f, 3000 D, Name, Sex), new Fanyafengbean (+, 18032278888f, hair, "Shian wind", true), new Fanyafengbea        N (%, Height, 15530124556f, 3000d, Name, Sex), new Fanyafengbean (+, +, Phone, 2000d, "Shian Wind", true)        }; /** * Enhanced for loop */for (Fanyafengbean Fanyafengbeanitem:fanyafengbeanarray) {System.ou T.print (Fanyafengbeanitem.getaGE () + ",");            System.out.print (Fanyafengbeanitem.getheight () + ",");            System.out.print (Fanyafengbeanitem.getphone () + ",");            System.out.print (Fanyafengbeanitem.gethair () + ",");            System.out.print (Fanyafengbeanitem.getname () + ",");            if (Fanyafengbeanitem.issex ()) {sex = "male";            } else {sex = "female";            } System.out.print (Sex);        System.out.println ();        } System.out.println ("-------------------------------------------------------------------");            /** * Normal for loop traversal * and some list,map * also have an array of custom classes */for (int i = 0; i < 4; i++) {            Fanyafengbean Fanyafengbean = new Fanyafengbean (age, Height, Phone, hair, Name, Sex);            map<string, object> map = new hashmap<string, object> ();            Map.put ("Myage", Fanyafengbeanarray[i].getage ()); System.out.print (Fanyafengbeanarray[i].getage () +",");            Map.put ("Myheight", Fanyafengbeanarray[i].getheight ());            System.out.print (Fanyafengbeanarray[i].getheight () + ",");            Map.put ("Myphone", Fanyafengbeanarray[i].getphone ());            System.out.print (Fanyafengbeanarray[i].getphone () + ",");            Map.put ("Myhair", Fanyafengbeanarray[i].gethair ());            System.out.print (Fanyafengbeanarray[i].gethair () + ",");            Map.put ("MyName", Fanyafengbeanarray[i].getname ());            System.out.print (Fanyafengbeanarray[i].getname () + ",");            Map.put ("Mysex", Fanyafengbeanarray[i].issex ());            if (Fanyafengbeanarray[i].issex ()) {sex = "male";            } else {sex = "female";            } System.out.print (Sex);            System.out.println ();        Fanyafenglist.add (map);        } arrays.sort (Fanyafengbeanarray);        Fanyafengbeans = Arrays.aslist (Fanyafengbeanarray);     System.out.println (Fanyafengbeans.tostring ());   System.out.println ("-------------------------------------------------------------------"); }
A third

Two-dimensional arrays

private static void Doublearray () {        /**         * Two-bit array */        int darray[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {0}}; for        (int i = 0, i < darray.length; i++) {for            (int j = 0; J < Darray[i].length; J + +) {                System.out.print (Darray[i][j]);            }            System.out.println ();        }        System.out.println ("-------------------------------------------------------------------");        for (int da[]: darray) {for            (int daitem:da) {                System.out.print (daitem);            }            System.out.println ();        }        System.out.println ("-------------------------------------------------------------------");    }

Fourth one:

This is the list array, which involves the use of iterators, traversing

    private static void list () {/** * list array * Iterator, enhanced for loop *        /list<string> list = new arraylist<> ();        List.add ("Shian Wind");        List.add ("learning");        List.add ("IOS");        iterator<string> Iterator = List.iterator ();        while (Iterator.hasnext ()) {            String strlist = (string) iterator.next ();            System.out.println (strlist);        }        System.out.println ("\ n" + "-------------------------------------------------------------------");        for (String item:list) {            System.out.print (item);        }        System.out.println ();        System.out.println ("-------------------------------------------------------------------");        for (int i = 0; i < list.size (); i++) {            System.out.print (List.get (i));        }        System.out.println ();        System.out.println ("-------------------------------------------------------------------");    }

Fifth one

Let's talk about the network layer writing common

See notes for details

private static void Linkmap () {/** * This emphasizes the need to initialize, otherwise the program is not going to get through * but I rarely initialize on some projects * It's really all defined first.        */String name = "Fanyafeng";        String major = "Software";        String url = "detail";        StringBuffer Myurl = new StringBuffer (URL);        StringBuffer MYURL1 = new StringBuffer (URL);        Myurl.append (":");        map<string, string> map = new linkedhashmap<string, string> ();        Map.put ("name", name);        Map.put ("Major", major);            For (map.entry<string, string> entry:map.entrySet ()) {Myurl.append (Entry.getkey ()). Append ("=");            Myurl.append (Entry.getvalue ());            Network acquisition Remember to use LINKEDHASHMAP, otherwise prone to problems//Myurl.append (Urlencoder.encode (Entry.getvalue (), "UTF-8"));        Myurl.append ("&");        } Myurl.deletecharat (Myurl.length ()-1);        System.out.print (Myurl.tostring ());       System.out.println ("\ n" + "--------------------------------------------"); map<string, string> map1 = new hashmap<string, string> ();        Map1.put ("name", name);        Map1.put ("Major", major);            For (map.entry<string, string> entry:map1.entrySet ()) {Myurl1.append (Entry.getkey ()). Append ("=");            Myurl1.append (Entry.getvalue ());        Myurl1.append ("&");        } Myurl.deletecharat (Myurl.length ()-1);        System.out.print (Myurl.tostring ());    System.out.println ("\ n" + "--------------------------------------------"); }
Sixth one

Use of iterators and maps

private static void It () {        /**         * iterator */        map map = new HashMap ();        for (int i = 0; i < i++) {            map.put (i, New String ("map" + i));        }        Iterator Iterator=map.entryset (). Iterator ();        while (Iterator.hasnext ()) {            map.entry strmap= (map.entry) Iterator.next ();            System.out.print (Strmap.getvalue () + ",");        }        System.out.println ("\ n" + "----------------------------------");    }

The seventh one is the main method, and the classes that need to be introduced

Import Java.io.unsupportedencodingexception;import Java.net.urlencoder;import Java.util.arraylist;import Java.util.arrays;import Java.util.hashmap;import Java.util.iterator;import Java.util.linkedhashmap;import Java.util.linkedlist;import java.util.list;import java.util.map;/** * Created by Fanyafeng on 2015/5/8/0008. */public class TestClass {public    static void Main (string[] args) {        System.out.println ("Test main program");        System.out.println ("-------------------------------------------------------------------");        Intorstring ();        Growing ();        Doublearray ();        List ();        Linkmap ();        It ();    }

and finally the program.









iterator&map&list&linkedhashmap& normal for loop & enhanced for Loop &bean

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.