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]);
- }
- 原文链接:http://coolshell.cn/articles/889.html
相关推荐
20个非常有用的Java程序片段20个非常有用的Java程序片段
下面是20个非常有用的Java程序片段,希望能对你有用。
20个非常有用的Java程序片段
JAVA 学习必备10个有用的程序片段 JAVA 学习必备10个有用的程序片段 JAVA 学习必备10个有用的程序片段 JAVA 学习必备10个有用的程序片段
介绍20个常用到的代码片段,如得到当前方法的名字、列出文件和目录、解析/读取XML 文件等
《java程序设计》课程的题库资料,由贺州学院整理,可供学生期末课程复习使用,也可以供相关任课教师出卷使用。 内容示例为: 9. 阅读下列程序片段,写出运行结果。 public class Test { public static void main...
主要为大家分享了20个非常实用的Java程序片段,对java开发项目有所帮助,感兴趣的小伙伴们可以参考一下
JAVA 代码 经典 经验
实验二Java面向对象程序设计 (1)定义表示课程的类Course,课程的属性包括课程名、编号、先修课号;方法包括设置课程名、设置编号、设置先修课号,以及打印课程名、课程号、先修课号。编写主程序,创建Course类的对象...
下面是20个非常有用的Java程序片段,希望能对你有用。
完整的代码示例,Java开发人员必备,涵盖日常Java开发经常要用到的功能代码。
20个常用的Java程序块 详细 //1. 字符串有整型的相互转换 002 String a =... java常用设计模式 java常用类 java常用包 java常用方法 java常用api java常用的..
* 4.创建DiagnosticCollector用来执行获取执行失败的错误结果 * 5.添加动态执行的编译环境 options 是个集合,添加内容,字符集,classpath等 * 6.传入JavaFileObject的java文件,是个集合,创建JavaSourceObject...
基于功能代码片段的Java后门检测方法.pdf
javamid处理一些 java 代码片段使用 java 实用程序来读取/写入/播放 MIDI 文件。
JAVA时钟小应用程序 代码片段 { Thread 时针=null, 分针=null,秒针=null; //用来表示时针、分针和秒针的线程。 //表示时针、分针、秒针端点的整型变量: int hour_a,hour_b,munite_a,munite_b,second_a,...
1给定如下Java程序,编译运行时,将在()出现错误。(选一项) public class Test{ public static void main(String[]args){ int i= 0; //a语句 for(;i;i++){ //b语句 if(i>5){ String test = “hello”; /...
这是java程序设计基础片段的课文代码和课后习题的代码,大部分都拥有
Java 根据数字报语音的程序 一个用JAVA写的语音报号程序,不过报出的是英文,不过你只要掌握了原理就好办多了,替换一下语音文件就可以了。它可以根据你输入的数字报出相对应的英文发音,实际上程序还是很简单的。