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

17. 把Array 转换成 Map

1.import java.util.Map;   
2.import org.apache.commons.lang.ArrayUtils;   
3.
4.public class Main {   
5.
6.  public static void main(String[] args) {   
7.    String[][] countries = { { "United States", "New York" }, { "United Kingdom", "London" },   
8.        { "Netherland", "Amsterdam" }, { "Japan", "Tokyo" }, { "France", "Paris" } };   
9.
10.    Map countryCapitals = ArrayUtils.toMap(countries);   
11.
12.    System.out.println("Capital of Japan is " + countryCapitals.get("Japan"));   
13.    System.out.println("Capital of France is " + countryCapitals.get("France"));   
14.  }   
15.}  

18. 发送邮件

1.import javax.mail.*;   
2.import javax.mail.internet.*;   
3.import java.util.*;   
4.
5.publicvoid postMail( String recipients[ ], String subject, String message , String from) throws MessagingException   
6.{   
7.boolean debug = false;   
8.
9.//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请求

1.import java.io.BufferedReader;   
2.import java.io.InputStreamReader;   
3.import java.net.URL;   
4.
5.publicclass Main {   
6.publicstaticvoid main(String[] args)  {   
7.try {   
8.            URL my_url = new URL("http://coolshell.cn/");   
9.            BufferedReader br = new BufferedReader(new InputStreamReader(my_url.openStream()));   
10.            String strTemp = "";   
11.while(null != (strTemp = br.readLine())){   
12.            System.out.println(strTemp);   
13.        }   
14.        } catch (Exception ex) {   
15.            ex.printStackTrace();   
16.        }   
17.    }   
18.

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.
19.// Test routine for resizeArray().  
20.publicstaticvoid main (String[] args) {   
21.int[] a = {1,2,3};   
22.   a = (int[])resizeArray(a,5);   
23.   a[3] = 4;   
24.   a[4] = 5;   
25.for (int i=0; i<a.length; i++)   
26.      System.out.println (a[i]);   

}

 

你可能感兴趣的:(java,程序,public,countries)