1、Java经典实例第二版Java经典实例(第二版)1. 获取环境变量Java代码 1. System.getenv(PATH);2. System.getenv(JAVA_HOME);System.getenv(PATH);System.getenv(JAVA_HOME);2. 获取系统属性Java代码 1. System.getProperty(pencilcolor);/得到属性值2. java-Dpencilcolor=green3. System.getProperty(java.specification.version);/得到Java版本号4. Propertiesp=System
2、.getProperties();/得到所有属性值5. p.list(System.out);System.getProperty(pencil color); / 得到属性值java -Dpencil color=greenSystem.getProperty(java.specification.version); / 得到Java版本号Properties p = System.getProperties(); / 得到所有属性值p.list(System.out);3. StringTokenizerJava代码 1. /能够同时识别,和|2. StringTokenizerst=ne
3、wStringTokenizer(Hello,World|of|Java,|);3. while(st.hasMoreElements()4. st.nextToken();5. 6. 7. /把分隔符视为token8. StringTokenizerst=newStringTokenizer(Hello,World|of|Java,|,true);/ 能够同时识别, 和 |StringTokenizer st = new StringTokenizer(Hello, World|of|Java, , |);while (st.hasMoreElements() st.nextToken();
4、/ 把分隔符视为tokenStringTokenizer st = new StringTokenizer(Hello, World|of|Java, , |, true);4. StringBuffer(同步)和StringBuilder(非同步)Java代码 1. StringBuildersb=newStringBuilder();2. sb.append(Hello);3. sb.append(World);4. sb.toString();5. newStringBuffer(a).reverse();/反转字符串StringBuilder sb = new StringBuilde
5、r();sb.append(Hello);sb.append(World);sb.toString();new StringBuffer(a).reverse(); / 反转字符串5.数字Java代码 1. /数字与对象之间互相转换-Integer转int2. Integer.intValue();3. 4. /浮点数的舍入5. Math.round()6. 7. /数字格式化8. NumberFormat9. 10. /整数-二进制字符串11. toBinaryString() 或valueOf()12. 13. /整数-八进制字符串14. toOctalString()15. 16. /整
6、数-十六进制字符串17. toHexString()18. 19. /数字格式化为罗马数字20. RomanNumberFormat()21. 22. /随机数23. Randomr=newRandom();24. r.nextDouble();25. r.nextInt();/ 数字与对象之间互相转换 - Integer转intInteger.intValue();/ 浮点数的舍入 Math.round()/ 数字格式化NumberFormat/ 整数 - 二进制字符串toBinaryString()或valueOf()/ 整数 - 八进制字符串toOctalString()/ 整数 - 十
7、六进制字符串toHexString()/ 数字格式化为罗马数字RomanNumberFormat()/ 随机数Random r = new Random();r.nextDouble();r.nextInt();6. 日期和时间Java代码 1. /查看当前日期2. Datetoday=newDate();3. Calendar.getInstance().getTime();4. 5. /格式化默认区域日期输出6. DateFormatdf=DateFormat.getInstance();7. df.format(today);8. 9. /格式化制定区域日期输出10. DateForma
8、tdf_cn=DateFormat.getDateInstance(DateFormat.FULL,Locale.CHINA);11. Stringnow=df_cn.format(today);12. 13. /按要求格式打印日期14. SimpleDateFormatsdf=newSimpleDateFormat(yyyy-MM-ddhh:mm:ss);15. sdf.format(today);16. 17. /设置具体日期18. GregorianCalendard1=newGregorianCalendar(2009,05,06);/6月6日19. GregorianCalendar
9、d2=newGregorianCalendar();/今天20. Calendard3=Calendar.getInstance();/今天21. d1.getTime();/Calendar或GregorianCalendar转成Date格式22. d3.set(Calendar.YEAR,1999);23. d3.set(Calendar.MONTH,Calendar.APRIL);24. d3.set(Calendar.DAY_OF_MONTH,12);25. 26. /字符串转日期27. SimpleDateFormatsdf=newSimpleDateFormat(yyyy-MM-d
10、dhh:mm:ss);28. Datenow=sdf.parse(String);29. 30. /日期加减31. Datenow=newDate();32. longt=now.getTime();33. t+=700*24*60*60*1000;34. Datethen=newDate(t);35. 36. Calendarnow=Calendar.getInstance();37. now.add(Calendar.YEAR,-2);38. 39. /计算日期间隔(转换成long来计算)40. today.getTime()-old.getTime();41. 42. /比较日期43.
11、Date 类型,就使用equals(),before(),after()来计算44. long类型,就使用=,来计算45. 46. /第几日47. 使用 Calendar的get()方法48. Calendarc=Calendar.getInstance();49. c.get(Calendar.YEAR);50. 51. /记录耗时52. longstart=System.currentTimeMillis();53. longend=System.currentTimeMillis();54. longelapsed=end-start;55. System.nanoTime();/毫秒5
12、6. 57. /长整形转换成秒58. Double.toString(t/1000D);/ 查看当前日期Date today = new Date();Calendar.getInstance().getTime();/ 格式化默认区域日期输出DateFormat df = DateFormat.getInstance();df.format(today);/ 格式化制定区域日期输出 DateFormat df_cn = DateFormat.getDateInstance(DateFormat.FULL, Locale.CHINA);String now = df_cn.format(tod
13、ay);/ 按要求格式打印日期SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd hh:mm:ss);sdf.format(today);/ 设置具体日期GregorianCalendar d1 = new GregorianCalendar(2009, 05, 06); / 6月6日GregorianCalendar d2 = new GregorianCalendar(); / 今天Calendar d3 = Calendar.getInstance(); / 今天d1.getTime(); / Calendar或Gregorian
14、Calendar转成Date格式d3.set(Calendar.YEAR, 1999);d3.set(Calendar.MONTH, Calendar.APRIL);d3.set(Calendar.DAY_OF_MONTH, 12);/ 字符串转日期SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd hh:mm:ss);Date now = sdf.parse(String);/ 日期加减Date now = new Date();long t = now.getTime();t += 700*24*60*60*1000;Date th
15、en = new Date(t);Calendar now = Calendar.getInstance();now.add(Calendar.YEAR, -2);/ 计算日期间隔(转换成long来计算)today.getTime() - old.getTime();/ 比较日期Date类型,就使用equals(), before(), after()来计算long类型,就使用=, 来计算/ 第几日使用Calendar的get()方法Calendar c = Calendar.getInstance();c.get(Calendar.YEAR); / 记录耗时long start = Syst
16、em.currentTimeMillis();long end = System.currentTimeMillis();long elapsed = end - start;System.nanoTime(); /毫秒/ 长整形转换成秒Double.toString(t/1000D);7. 结构化数据Java代码 1. /数组拷贝2. System.arrayCopy(oldArray,0,newArray,0,oldArray.length);3. 4. /ArrayList5. add(Objecto)/在末尾添加给定元素6. add(inti,Objecto)/在指定位置插入给定元素7
17、. clear()/从集合中删除全部元素8. Contains(Objecto)/如果Vector包含给定元素,返回真值9. get(inti)/返回 指定位置的对象句柄10. indexOf(Objecto)/如果找到给定对象,则返回其索引值;否则,返回-111. remove(Objecto)/根据引用删除对象12. remove(inti)/根据 位置删除对象13. toArray()/返回包含集合对象的数组14. 15. /Iterator16. Listlist=newArrayList();17. Iteratorit=list.iterator();18. while(it.ha
18、sNext()19. Objecto=it.next();20. 21. /链表22. LinkedListlist=newLinkedList();23. ListIteratorit=list.listIterator();24. while(it.hasNext()25. Objecto=it.next();26. 27. /HashMap28. HashMaphm=newHashMap();29. hm.get(key);/通过key得到value30. hm.put(No1,Hexinyu);31. hm.put(No2,Sean);32. /方法1:获取全部键值33. Iterat
19、orit=hm.values().iterator();34. while(it.hasNext()35. StringmyKey=it.next();36. StringmyValue=hm.get(myKey);37. 38. /方法2:获取全部键值39. for(Stringkey:hm.keySet()40. StringmyKey=key;41. StringmyValue=hm.get(myKey);42. 43. 44. /Preferences-与系统相关的用户设置,类似名-值对45. Preferencesprefs=Preferences.userNodeForPackag
20、e(ArrayDemo.class);46. Stringtext=prefs.get(textFontName,lucida-bright);47. Stringdisplay=prefs.get(displayFontName,lucida-balckletter);48. System.out.println(text);49. System.out.println(display);50. /用户设置了新值,存储回去51. prefs.put(textFontName,new-bright);52. prefs.put(displayFontName,new-balckletter);
21、53. 54. /Properties-类似名-值对,key和value之间,可以用=,:或空格分隔,用# 和!注释55. InputStreamin=MediationServer.class.getClassLoader().getResourceAsStream(msconfig.properties);56. Propertiesprop=newProperties();57. prop.load(in);58. in.close();59. prop.setProperty(key,value);60. prop.getProperty(key);61. 62. /排序63. 1.数
22、组:Arrays.sort(strings);64. 2.List:Collections.sort(list);65. 3.自定义类:classSubCompimplementsComparator66. 然 后使用Arrays.sort(strings,newSubComp()67. 68. /两个接口69. 1.java.lang.Comparable:提供对象的自然排序,内置于类中70. intcompareTo(Objecto);71. booleanequals(Objecto2);72. 2.java.util.Comparator:提供特定的比较方法73. intcompare
23、(Objecto1,Objecto2)74. 75. /避免重复排序,可以使用TreeMap76. TreeMapsorted=newTreeMap(unsortedHashMap);77. 78. /排除重复元素79. Hashseths-newHashSet();80. 81. /搜索对象82. binarySearch():快 速查询-Arrays,Collections83. contains():线型搜 索-ArrayList,HashSet,Hashtable,linkedList,Properties,Vector84. containsKey():检 查集合对象是否包含给定-H
24、ashMap,Hashtable,Properties,TreeMap85. containsValue():主 键(或给定值)-HashMap,Hashtable,Properties,TreeMap86. indexOf():若 找到给定对象,返回其位置-ArrayList,linkedList,List,Stack,Vector87. search():线 型搜素-Stack88. 89. /集合转数组90. toArray();91. 92. /集合总结93. Collection:Set-HashSet,TreeSet94. Collection:List-ArrayList,Vec
25、tor,LinkedList95. Map:HashMap,HashTable,TreeMap/ 数组拷贝System.arrayCopy(oldArray, 0, newArray, 0, oldArray.length);/ ArrayListadd(Object o) / 在末尾添加给定元素add(int i, Object o) / 在指定位置插入给定元素clear() / 从集合中删除全部元素Contains(Object o) / 如果Vector包含给定元素,返回真值get(int i) / 返回指定位置的对象句柄indexOf(Object o) / 如果找到给定对象,则返回其
26、索引值;否则,返回-1remove(Object o) / 根据引用删除对象remove(int i) / 根据位置删除对象toArray() / 返回包含集合对象的数组/ IteratorList list = new ArrayList();Iterator it = list.iterator();while (it.hasNext()Object o = it.next();/ 链表LinkedList list = new LinkedList();ListIterator it = list.listIterator();while (it.hasNext()Object o =
27、it.next();/ HashMapHashMap hm = new HashMap();hm.get(key); / 通过key得到valuehm.put(No1, Hexinyu);hm.put(No2, Sean);/ 方法1: 获取全部键值 Iterator it = hm.values().iterator(); while (it.hasNext() String myKey = it.next(); String myValue = hm.get(myKey);/ 方法2: 获取全部键值 for (String key : hm.keySet() String myKey = key; String myValue = hm.get(myKey);/ Preferenc
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1