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.改变数组的大年夜小
阿里Java高等大年夜牛直播讲解常识点,分享常识,膳绫擎几大年夜专题都是各位师长教师多年工作经验的梳理和总结,带着大年夜家周全、科学地建立本身的技巧体系和技巧认知!
/**Reallocates an array with a new size, and copies the contentsof 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]); }
【编辑推荐】
- 扎心了,老铁!下班回家无人陪,法度榜样员又双叒叕成独身单身率最高!
- Facebook为Android法度榜样员增长开源新特点,大年夜幅度修改Buck!
- 阿里专家:技巧变更那么快,法度榜样员若何做到不被镌汰?
- 嫁给法度榜样员老公,我“懊悔”了!
- 研究了1000名法度榜样员发明,月薪8K与月薪3W的法度榜样员本来差距在这里
推荐阅读
Tech Neo技巧沙龙 | 11月25号,九州云/ZStack与您一路商量云时代收集界线治理实践 图1 广东生益科技信息治理部CIO周嘉林 广东生益科技股份有限公司创建于1985年,拥有员工3800多名,是>>>详细阅读
本文标题:程序员花2小时总结:20个非常有用的Java程序片段
地址:http://www.17bianji.com/lsqh/38914.html
1/2 1