17. 把 Array 转换成 Map
view source <embed id="highlighter_896067_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://cocre.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1243315889047" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_896067" menu="false"></embed> print ? 01. 02. import java.util.Map; 03. import org.apache.commons.lang.ArrayUtils; 04. 05. public class Main { 06. 07. public static void main(String[] args) { 08. String[][] countries = { { "United States" , "New York" }, { "United Kingdom" , "London" }, 09. { "Netherland" , "Amsterdam" }, { "Japan" , "Tokyo" }, { "France" , "Paris" } }; 10. 11. Map countryCapitals = ArrayUtils.toMap(countries); 12. 13. System.out.println( "Capital of Japan is " + countryCapitals.get( "Japan" )); 14. System.out.println( "Capital of France is " + countryCapitals.get( "France" )); 15. } 16. }18. 发送邮件
view source <embed id="highlighter_191237_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://cocre.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1243315889051" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_191237" menu="false"></embed> print ? 01. import javax.mail.*; 02. import javax.mail.internet.*; 03. import java.util.*; 04. 05. public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException 06. { 07. boolean debug = false ; 08. 09. //Set the host smtp address 10. Properties props = new Properties(); 11. props.put( "mail.smtp.host" , "smtp.example.com" ); 12. 13. // create some properties and get the default Session 14. Session session = Session.getDefaultInstance(props, null ); 15. session.setDebug(debug); 16. 17. // create a message 18. Message msg = new MimeMessage(session); 19. 20. // set the from and to address 21. InternetAddress addressFrom = new InternetAddress(from); 22. msg.setFrom(addressFrom); 23. 24. InternetAddress[] addressTo = new InternetAddress[recipients.length]; 25. for ( int i = 0 ; i < recipients.length; i++) 26. { 27. addressTo[i] = new InternetAddress(recipients[i]); 28. } 29. msg.setRecipients(Message.RecipientType.TO, addressTo); 30. 31. // Optional : You can also set your custom headers in the Email if you Want 32. msg.addHeader( "MyHeaderName" , "myHeaderValue" ); 33. 34. // Setting the Subject and Content Type 35. msg.setSubject(subject); 36. msg.setContent(message, "text/plain" ); 37. Transport.send(msg); 38. }19. 发送代数据的HTTP 请求
view source <embed id="highlighter_334990_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://cocre.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1243315889055" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_334990" menu="false"></embed> print ? 01. 02. import java.io.BufferedReader; 03. import java.io.InputStreamReader; 04. import java.net.URL; 05. 06. public class Main { 07. public static void main(String[] args) { 08. try { 09. URL my_url = new URL( "http://cocre.com/ " ); 10. BufferedReader br = new BufferedReader( new InputStreamReader(my_url.openStream())); 11. String strTemp = "" ; 12. while ( null != (strTemp = br.readLine())){ 13. System.out.println(strTemp); 14. } 15. } catch (Exception ex) { 16. ex.printStackTrace(); 17. } 18. } 19. }20. 改变数组的大小
view source <embed id="highlighter_818592_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://cocre.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1243315889059" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_818592" menu="false"></embed> print ? 01. 02. /** 03. * Reallocates an array with a new size, and copies the contents 04. * of the old array to the new array. 05. * @param oldArray the old array, to be reallocated. 06. * @param newSize the new array size. 07. * @return A new array with the same contents. 08. */ 09. private static Object resizeArray (Object oldArray, int newSize) { 10. int oldSize = java.lang.reflect.Array.getLength(oldArray); 11. Class elementType = oldArray.getClass().getComponentType(); 12. Object newArray = java.lang.reflect.Array.newInstance( 13. elementType,newSize); 14. int preserveLength = Math.min(oldSize,newSize); 15. if (preserveLength > 0 ) 16. System.arraycopy (oldArray, 0 ,newArray, 0 ,preserveLength); 17. return newArray; 18. } 19. 20. // Test routine for resizeArray(). 21. public static void main (String[] args) { 22. int [] a = { 1 , 2 , 3 }; 23. a = ( int [])resizeArray(a, 5 ); 24. a[ 3 ] = 4 ; 25. a[ 4 ] = 5 ; 26. for ( int i= 0 ; i<a.length; i++) 27. System.out.println (a[i]); 28. } 相关资源:敏捷开发V1.0.pptx