20非常有用的Java程序片段(4)

17. 把 Array 转换成 Map

  
  
  
  
  1. import java.util.Map;  

  2. import org.apache.commons.lang.ArrayUtils;  

  3. public class Main {  

  4.  public static void main(String[] args) {  

  5.    String[][] countries = { { "United States", "New York" }, { "United Kingdom", "London" },  

  6.        { "Netherland", "Amsterdam" }, { "Japan", "Tokyo" }, { "France", "Paris" } };  

  7.    Map countryCapitals = ArrayUtils.toMap(countries);  

  8.    System.out.println("Capital of Japan is " + countryCapitals.get("Japan"));  

  9.    System.out.println("Capital of France is " + countryCapitals.get("France"));  

  10.  }  

  11. }  

18. 发送邮件

  
  
  
  
  1. import javax.mail.*;  

  2. import javax.mail.internet.*;  

  3. import java.util.*;  

  4. publicvoid postMail( String recipients[ ], String subject, String message , String from) throws MessagingException  

  5. {  

  6. boolean debug = false;  

  7. //Set the host smtp address  

  8.     Properties props = new Properties();  

  9.     props.put("mail.smtp.host", "smtp.example.com");  

  10. // create some properties and get the default Session  

  11.    Session session = Session.getDefaultInstance(props, null);  

  12.    session.setDebug(debug);  

  13. // create a message  

  14.    Message msg = new MimeMessage(session);  

  15. // set the from and to address  

  16.    InternetAddress addressFrom = new InternetAddress(from);  

  17.    msg.setFrom(addressFrom);  

  18.    InternetAddress[] addressTo = new InternetAddress[recipients.length];  

  19. for (int i = 0; i < recipients.length; i++)  

  20.    {  

  21.        addressTo[i] = new InternetAddress(recipients[i]);  

  22.    }  

  23.    msg.setRecipients(Message.RecipientType.TO, addressTo);  

  24. // Optional : You can also set your custom headers in the Email if you Want  

  25.    msg.addHeader("MyHeaderName", "myHeaderValue");  

  26. // Setting the Subject and Content Type  

  27.    msg.setSubject(subject);  

  28.    msg.setContent(message, "text/plain");  

  29.    Transport.send(msg);  

  30. }  

19. 发送代数据的HTTP 请求

  
  
  
  
  1. import java.io.BufferedReader;  

  2. import java.io.InputStreamReader;  

  3. import java.net.URL;  

  4. publicclass Main {  

  5. publicstaticvoid main(String[] args)  {  

  6. try {  

  7.            URL my_url = new URL("http://coolshell.cn/");  

  8.            BufferedReader br = new BufferedReader(new InputStreamReader(my_url.openStream()));  

  9.            String strTemp = "";  

  10. while(null != (strTemp = br.readLine())){  

  11.            System.out.println(strTemp);  

  12.        }  

  13.        } catch (Exception ex) {  

  14.            ex.printStackTrace();  

  15.        }  

  16.    }  

  17. }

20. 改变数组的大小

  
  
  
  
  1. /**

  2. * Reallocates an array with a new size, and copies the contents

  3. * of the old array to the new array.

  4. * @param oldArray  the old array, to be reallocated.

  5. * @param newSize   the new array size.

  6. * @return          A new array with the same contents.

  7. */

  8. privatestatic Object resizeArray (Object oldArray, int newSize) {  

  9. int oldSize = java.lang.reflect.Array.getLength(oldArray);  

  10.   Class elementType = oldArray.getClass().getComponentType();  

  11.   Object newArray = java.lang.reflect.Array.newInstance(  

  12.         elementType,newSize);  

  13. int preserveLength = Math.min(oldSize,newSize);  

  14. if (preserveLength > 0)  

  15.      System.arraycopy (oldArray,0,newArray,0,preserveLength);  

  16. return newArray;  

  17. }  

  18. // Test routine for resizeArray().  

  19. publicstaticvoid main (String[] args) {  

  20. int[] a = {1,2,3};  

  21.   a = (int[])resizeArray(a,5);  

  22.   a[3] = 4;  

  23.   a[4] = 5;  

  24. for (int i=0; i<a.length; i++)  

  25.      System.out.println (a[i]);  

  26. }

原文链接:http://coolshell.cn/articles/889.html

【编辑推荐】

  1. 每位设计师都应该拥有的50个CSS代码片段

  2. 如何写出无法维护的代码

  3. 代码里的命名规则:错误的和正确的对比

  4. .NET代码转换器

【责任编辑: chensf TEL:(010)68476606】



你可能感兴趣的:(java)