20非常有用的Java程序片段(4)
来源:互联网 发布:尔雅公选课软件 编辑:程序博客网 时间:2024/05/17 23:11
17. 把 Array 转换成 Map
- import java.util.Map;
- import org.apache.commons.lang.ArrayUtils;
- public class Main {
- public static void main(String[] args) {
- String[][] countries = { { "United States", "New York" }, { "United Kingdom", "London" },
- { "Netherland", "Amsterdam" }, { "Japan", "Tokyo" }, { "France", "Paris" } };
- Map countryCapitals = ArrayUtils.toMap(countries);
- System.out.println("Capital of Japan is " + countryCapitals.get("Japan"));
- System.out.println("Capital of France is " + countryCapitals.get("France"));
- }
- }
18. 发送邮件
- import javax.mail.*;
- import javax.mail.internet.*;
- import java.util.*;
- public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
- {
- boolean debug = false;
- //Set the host smtp address
- Properties props = new Properties();
- props.put("mail.smtp.host", "smtp.example.com");
- // create some properties and get the default Session
- Session session = Session.getDefaultInstance(props, null);
- session.setDebug(debug);
- // create a message
- Message msg = new MimeMessage(session);
- // set the from and to address
- InternetAddress addressFrom = new InternetAddress(from);
- msg.setFrom(addressFrom);
- InternetAddress[] addressTo = new InternetAddress[recipients.length];
- for (int i = 0; i < recipients.length; i++)
- {
- addressTo[i] = new InternetAddress(recipients[i]);
- }
- msg.setRecipients(Message.RecipientType.TO, addressTo);
- // Optional : You can also set your custom headers in the Email if you Want
- msg.addHeader("MyHeaderName", "myHeaderValue");
- // Setting the Subject and Content Type
- msg.setSubject(subject);
- msg.setContent(message, "text/plain");
- Transport.send(msg);
- }
19. 发送代数据的HTTP 请求
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.URL;
- public class Main {
- public static void main(String[] args) {
- try {
- URL my_url = new URL("http://coolshell.cn/");
- BufferedReader br = new BufferedReader(new InputStreamReader(my_url.openStream()));
- String strTemp = "";
- while(null != (strTemp = br.readLine())){
- System.out.println(strTemp);
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
20. 改变数组的大小
- /**
- * Reallocates an array with a new size, and copies the contents
- * of the old array to the new array.
- * @param oldArray the old array, to be reallocated.
- * @param newSize the new array size.
- * @return A new array with the same contents.
- */
- private static Object resizeArray (Object oldArray, int newSize) {
- int oldSize = java.lang.reflect.Array.getLength(oldArray);
- Class elementType = oldArray.getClass().getComponentType();
- Object newArray = java.lang.reflect.Array.newInstance(
- elementType,newSize);
- int preserveLength = Math.min(oldSize,newSize);
- if (preserveLength > 0)
- System.arraycopy (oldArray,0,newArray,0,preserveLength);
- return newArray;
- }
- // Test routine for resizeArray().
- public static void main (String[] args) {
- int[] a = {1,2,3};
- a = (int[])resizeArray(a,5);
- a[3] = 4;
- a[4] = 5;
- for (int i=0; i<a.length; i++)
- System.out.println (a[i]);
- }
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段
- 20非常有用的Java程序片段(2)
- 【Java】JTable组件的构造函数和设置列宽
- 黑马程序员_Date 与Calendar简单讲解
- 20非常有用的Java程序片段(3)
- C++ 运行时类型识别RTTI typeid<->dynamic_cast
- 20非常有用的Java程序片段(4)
- linux的时间相关函数
- MSDN 目录内容很多空白
- poj2075Tangled in Cables (最小生成树之prim)
- POJ 3259 最短路 判负环
- 小学期练习一(高级班) C - Holiday's Accommodation
- 几种典型的 iOS 应用界面的交互框架各自的优缺点
- 将一个NSImage实例保存成图片文件
- 01 背包基础题目