Array, java Array

Source: Internet
Author: User

Array, java Array

(The package name uses Chinese characters. Do not care about this)

1. Array

1.

2. Enhanced

Package Yang bin_array; public class ArrayDemo {/** array Traversal method: 1.for 2. enhance for */public static void main (String [] args) {int [] arr = {1, 2, 3, 4, 5}; test1 (); test2 (arr);} // enhance forprivate static void test2 (int [] arr) {for (int I: arr) {System. out. println (I) ;}// forprivate static void test1 () {int [] arr = {1, 2, 3, 4, 5}; for (int I = 0; I <arr. length; I ++) {System. out. println (arr [I]) ;}}

2. Collection

1.

2. Enhanced

3. iterator

4. toArray

Package Yang Bin _ Collection; import java. util. arrayList; import java. util. collection; import java. util. iterator;/** traversal mode: 1. for (NO) 2, enhanced for 3. iterator 4. toArray */public class CollectionDemo {public static void main (String [] args) {Collection <String> c = new ArrayList <String> (); c. add ("Yang Bin"); c. add ("guo Lele"); c. add ("Kobe"); c. add ("Jordan"); // test1 (c); // test2 (c); test3 (c);} // toArrayprivate static void test3 (Collection <String> c) {Object [] cc = c. toArray (); for (int I = 0; I <cc. length; I ++) {System. out. println (cc [I]) ;}// Iterator private static void test2 (Collection <String> c) {Iterator <String> it = c. iterator (); while (it. hasNext () {System. out. println (it. next () ;}// enhance FORprivate static void test1 (Collection <String> c) {for (String c1: c) {System. out. println (c1 );}}}

3. List

Package array _ List; import java. util. arrayList; import java. util. iterator; import java. util. list; import java. util. listIterator;/** for * enhancement for * iterator * list-specific iterator * badge get () * toArray () */public class ListDemo {public static void main (String [] args) {List <String> c = new ArrayList <String> (); c. add ("Yang Bin"); c. add ("guo Lele"); c. add ("Kobe"); c. add ("Jordan"); // test1 (c); // test2 (c); // test3 (c); // test4 (c); test5 (c ); test6 (c);} private static void test6 (List <String> c) {for (ListIterator <String> operator = c. listIterator (); iterator. hasNext ();) {System. out. println (bytes. next () ;}// toArray () private static void test5 (List <String> c) {Object [] array = c. toArray (); for (int I = 0; I <array. length; I ++) {System. out. println (array [I]) ;}// badge get (); private static void test4 (List <String> c) {for (int I = 0; I <c. size (); I ++) {System. out. println (c. get (I) ;}}// LIst iterator private static void test3 (List <String> c) {ListIterator <String> lsc = c. listIterator (); while (lsc. hasNext () {System. out. println (lsc. next () ;}// Iterator private static void test2 (List <String> c) {Iterator <String> it = c. iterator (); while (it. hasNext () {System. out. println (it. next () ;}// enhance forprivate static void test1 (List <String> c) {for (String c1: c) {System. out. println (c1 );}}}

3. Set

Package Yang Bin _ Set; import java. util. hashSet; import java. util. iterator; import java. util. set;/** iterator * enhancement for * toArray **/public class SetList {public static void main (String [] args) {Set <String> c = new HashSet <String> (); c. add ("Yang Bin"); c. add ("guo Lele"); c. add ("Kobe"); c. add ("Jordan"); // test2 (c); // test1 (c); test3 (c);} // toArray () private static void test3 (Set <String> c) {Object [] array = c. toArray (); for (int I = 0; I <array. length; I ++) {System. out. println (array [I]) ;}// enhance forprivate static void test2 (Set <String> c) {for (String s: c) {System. out. println (s) ;}}// Iterator private static void test1 (Set <String> c) {Iterator <String> it = c. iterator (); while (it. hasNext () {System. out. println (it. next ());}}}

4. Map

Package Yang Bin _ Map; import java. util. collection; import java. util. hashMap; import java. util. map; import java. util. set; import java. util. map. entry;/* VAlues * keyset * entryset */public class MapDemo {public static void main (String [] args) {Map <Integer, String> c = new HashMap <Integer, string> (); c. put (1, "Yang Bin"); c. put (2, "Guo Lele"); c. put (3, "Kobe"); c. put (4, "Jordan"); // test1 (c); // test2 (c); test3 (c);} private static void test3 (Map <Integer, string> c) {for (Entry <Integer, String> entry: c. entrySet () {System. out. println (entry. getKey () + ":" + entry. getValue () ;}// keysetprivate static void test2 (Map <Integer, String> c) {Set <Integer> set = c. keySet (); for (Integer key: set) {String value1 = c. get (key); System. out. println (key + ":" + value1) ;}// Values can only be set to private static void test1 (Map <Integer, String> c) {Collection <String> co = c. values (); for (String s: co) {System. out. println (s );}}}


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.