java工具包.doc_第1页
java工具包.doc_第2页
java工具包.doc_第3页
java工具包.doc_第4页
java工具包.doc_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1. Java工具概述很多人初学程序时,总是在想,那么多的算法该怎么写呀?那么多的数据结构都不熟悉,该怎么实现呀?总是担心英语不好程序学不精通,数学不好写程序无法达到巅峰。学的程序越多,不懂的知识越多。这种想法很正常,毕竟传统的计算机教育都是从原理开始的,科学原理一般理解起来还能够接受,但是实现起来都很难。计算机发展到了今天,能成为原理的基本已经有人实现了,今天我们学习任何知识都是站在巨人的肩膀上,只要理解程序运行原理,算法的功能即可。底层的各种算法,各种数据结构已经被“巨人们”实现了,一般都放在程序开发类库中,程序员开发过程中直接调用即可。比如现在木工做家具,已经不存在自己砍树、加工木板、一点一点的雕刻了,如果需要木板,直接到市场上购买,需要各种图案,直接到市场购买,木工的工作就是把这些木板修理一下组装成一套家具即可。“工欲善其事,必先利其器”,在Java程序开发过程中,很多算法(比如:MD5加密算法)、很多数据结构(比如链表LinkedList)已经实现并且大多放在类库的java.util包中,程序员只需要了解各种工具的功能就可以直接调用。比如对一个数组进行排序,程序员可以写如下排序算法:代码演示:数组排序public static void sort(int arrs) boolean isSwap = false;for (int i = 0; i i; j-) if (arrsj - 1 arrsj) isSwap = true;int tmp = arrsj - 1;arrsj - 1 = arrsj;arrsj = tmp;该排序算法中只能对整数数组排序,还有其他数据类型呢?就需要重载很多方法进行排序操作。而在Java类库中有一个Arrays类的sort方法已经实现各种数据类型的排序算法。程序员只需要调用该类的方法即可。代码演示:Arrays实现排序public static void main(String args) int ages=23, 45,12,76,34,56,24;Arrays.sort(ages);for (int i = 0; i ages.length; i+) System.out.println(agesi);在Java开发类库中,提供了很多工具类,我们即将学习最常见的工具类,比如对日期的操作,对集合的操作等。具体更多的工具类,请参考JavaDoc文档。2. java.util.Date类Date类包装了毫秒值,毫秒值表示自1970年1月1日00:00:00 GMT开始到现在经过的毫秒数。该类的大部分构造器和方法都已经过时,但是该类使用非常方便,因此目前使用还很普遍,该类的另一个主要功能是,在数据库操作中,它允许将毫秒值表示为SQL DATE值,是数据库操作中java.sql.Date的父类。关于数据库操作,将在第八章开始讲解。该类目前推荐使用的构造方法有两个:构造方法说明Date()按照当前系统时间构造一个Date对象。Date(long date)按照给定的时间毫秒值构造一个 Date 对象。表1 java.util.Date类的构造方法主要的方法有:返回异常说明booleanafter(Date when)测试当前对象表示的时间是否在指定时间之后。booleanbefore(Date when)测试当前对象表示的时间是否在指定时间之前。longgetTime()返回当前对象对应的时间毫秒值voidsetTime(long time)设置时间表2 java.util.Date类的主要方法代码演示:时间设置public class Demo2 public static void main(String args) Date date=new Date(); date.setTime(10L*365+2)*24*60*60*1000); System.out.println(date); 代码解析: 构造当前系统时间。 设置时间值为1970年后10年的时间的毫秒值,10年间有2个闰年,10年的天数是:10*365+2,10L表示当前值是long类型。 调用Date的toString方法输出结果。代码输出结果:Tue Jan 01 08:00:00 CST 1980Q 老师,时间毫秒值从1970年1月1日0:00.000开始计算,上面示例中10年后应该是1980年1月1日0:00.000,为什么输出结果是:1980年1月1日 8:00呢?A java.util.Date类型表示的是GMT时间,本身输出是国际化输出,由于中国处于东八区时间,因此输出结果是早上8点。而Date的其他构造方法和普通方法的API都不容易实现国际化,因此目前Date类的大多数方法都被标识为过时,表示更灵活的时间类请参考java.util.Calendar。Date的输出结果是按照国际通用格式输出的,而中国更习惯于“年-月-日”的形式输出,这种特殊格式的输出需要用到Java格式化工具。3. 格式化工具格式化的目的是把一个对象以不同的格式表示,以满足不同环境对格式的要求,比如:前面学习的Date对象实质是一个以毫秒值表示的时间,但是在不同的国家和地区表示方式不一样。那么就需要对Date进行格式化处理。接下来主要学习Java对日期时间的格式化和对数字的格式化处理。 日期时间格式化Date类中包含了日期和时间,在Java编程中,日期通常指年、月、日,时间则指时、分、秒、毫秒。Java对Date进行格式化使用java.text.DateFormat类。在格式表示中,经常采用4种格式,这四种格式被定义为DateFormat类的常量。下表所示:格式说明SHORT以最短的格式表示,比如:09-8-20MEDIUM比short完整表示方式,比如:2009-8-20LONG比medium更完整的表示方式,比如:2009年8月20日FULL综合的表示方式,比如:2009年8月20日 星期四表3 DateFormat的四种表示格式因为不同国家地区需要格式化的结果不同,Locale类的对象表示了不同的区域,Locale定义目前全世界几乎所有地区的对象表示,比如:格式说明Locale.CHINA中国地区Locale.US美国地区Locale.FRANCE法国地区Locale.CANADA加拿大地区表4 Locale对部分地区的表示DateFormat是一个抽象类,不能直接实例化,可以使用下表中的静态方法得到DateFormat的对象。方法说明getDateInstance()返回默认地区,默认格式的关于日期的DateFormat对象。getDateInstance(int)返回指定格式下,默认地区的关于日期的DateFormat对象。getDateInstance(int, Locale)返回指定格式,指定地区的关于日期的DateFormat对象。getTimeInstance()返回默认地区,默认格式的关于时间的DateFormat对象。getTimeInstance (int)返回默认地区,指定格式的关于时间的DateFormat对象。getTimeInstance (int, Locale)返回指定地区,指定格式的关于时间的DateFormat对象。getDateTimeInstance()返回默认地区、默认日期格式、默认时间格式的关于日期和时间的DateFormat对象。getDateTimeInstance (int,int)返回默认地区、指定日期格式、指定时间格式的关于日期和时间的DateFormat对象。getDateTimeInstance (int,int, Locale)返回指定地区、指定日期格式、指定时间格式的关于日期和时间的DateFormat对象。表5 获取DateFormat对象的静态方法调用DateFormat对象的format方法可以把Date对象转换成为指定格式的String类型数据。比如:Date today=new Date();DateFormat df=DateFormat.getDateInstance(DateFormat.FULL,Locale.CHINA);String result=df.format(today);代码演示:日期的不同格式import java.text.DateFormat;import java.util.Date;import java.util.Locale;public class Demo3 public static void main(String args) Date today = new Date();Locale locals = new Locale Locale.CHINA, Locale.US, Locale.UK ;for (int i = 0; i locals.length; i+) DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT,localsi);DateFormat df2 = DateFormat.getDateInstance(DateFormat.MEDIUM,localsi);DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG,localsi);DateFormat df4 = DateFormat.getDateInstance(DateFormat.FULL,localsi);System.out.println(localsi.getDisplayCountry() + 的日期形式:);System.out.println(tShort格式: + df1.format(today);System.out.println(tMedium格式: + df2.format(today);System.out.println(tLong格式: + df3.format(today);System.out.println(tFull格式: + df4.format(today);代码输出结果:中国的日期形式:Short格式:09-8-20Medium格式:2009-8-20Long格式:2009年8月20日Full格式:2009年8月20日 星期四美国的日期形式:Short格式:8/20/09Medium格式:Aug 20, 2009Long格式:August 20, 2009Full格式:Thursday, August 20, 2009英国的日期形式:Short格式:20/08/09Medium格式:20-Aug-2009Long格式:20 August 2009Full格式:20 August 2009在Java程序设计过程中,对应日期和时间的格式化,还有一个简单的格式化方式,就是java.text.SimpleDateFormat,该类中用字符串指定日期和时间的格式,字符串中的字符称为模式字符,模式字符区分大小写。常见的模式字符定义如下:字母日期或时间元素y年M年中的月份w年中的周数W月份中的周数D年中的天数d月份中的天数F月份中的星期E星期中的天数aAm/pm 标记H一天中的小时数(0-23)k一天中的小时数(1-24)Kam/pm 中的小时数(0-11)ham/pm 中的小时数(1-12)m小时中的分钟数s分钟中的秒数S毫秒数表6 模式字符串例如:日期和时间模式结果EEE, MMM d, yyWed, Jul 4, 01h:mm a12:08 PMyyyy-MM-dd HH:mm:ss2009-8-20 14:22yyyy年MM月dd HH:mm:ss2009年8月20 14:22:23表7 模式字符串示例SimpleDateFormat是DateFormat的子类,用法和DateFormat类基本一致,主要使用format()方法。代码演示:SimpleDateFormat进行日期转换import java.text.SimpleDateFormat;import java.util.Date;public class Demo4 public static void main(String args) Date today = new Date();SimpleDateFormat format1 = new SimpleDateFormat(yyyy-MM-dd);SimpleDateFormat format2 = new SimpleDateFormat(yyyy年MM月dd HH:mm:ss);SimpleDateFormat format3 = new SimpleDateFormat(HH:mm:ss);SimpleDateFormat format4 = new SimpleDateFormat(yyyy);System.out.println(format1.format(today);System.out.println(format2.format(today);System.out.println(format3.format(today);System.out.println(format4.format(today);代码输出结果:2009-08-202009年08月20 14:25:5814:25:582009在程序设计时,界面上用户输入的基本上都是字符串,如果字符串输入一个出生年月,如何把该字符串转换成Date类型呢?可以使用SimpleDateFormat的parse()方法。代码演示:SimpleDateFormat解析日期import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class Demo5 public static void main(String args) String birthday=1980-04-16;SimpleDateFormat format=new SimpleDateFormat(yyyy-MM-dd);try Date bir=format.parse(birthday);System.out.println(bir); catch (ParseException e) / TODO Auto-generated catch blocke.printStackTrace();代码解析: 用SimpleDateFormat解析日期的时候需要处理其中的ParseException异常。 数字格式化对数字的格式化,在程序处理中也是非常常用的,数字格式化主要对小数点位数,表示的形式(比如:百分数表示)等格式处理。NumberFormat 是所有数值格式的抽象基类。此类提供格式化和解析数值的接口。若要格式化当前Locale的数值,可使用其中一个方法:myString = NumberFormat.getInstance().format(myNumber);若要格式化不同 Locale 的日期,可在调用getInstance方法时指定它。NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);方法说明getInstance()获取常规数值格式。可以指定Local参数。getNumberInstance()获取常规数值格式。可以指定Local参数。getIntegerInstance()获取整数数值格式。可以指定Local参数。getCurrencyInstance ()获取货币数值格式。可以指定Local参数。格式化后的数据前面会有一个货币符号,比如:“¥”getPercentInstance()获取显示百分比的格式。可以指定Local参数。比如:小数 0.53 将显示为 53%。表8 获取NumberFormat对象代码演示:NumberFormat进行数字格式化import java.text.DecimalFormat;import java.text.NumberFormat;import java.util.Locale;public class Demo6 public static void main(String args) double mynum1 = 230456789;double mynum2 = 0.23;NumberFormat nf1 = NumberFormat.getInstance(Locale.CHINA);NumberFormat nf2 = NumberFormat.getCurrencyInstance(Locale.CHINA);NumberFormat nf3 = NumberFormat.getCurrencyInstance(Locale.US);NumberFormat nf4 = NumberFormat.getPercentInstance();System.out.println(nf1.format(mynum1);System.out.println(nf2.format(mynum1);System.out.println(nf3.format(mynum1);System.out.println(nf4.format(mynum2);代码输出结果:230,456,789¥230,456,789.00$230,456,789.0023%关于更复杂的数字格式化,可以使用java.text.DecimalFormat进行处理,该类通过模式字符串对数字格式化。代码演示:DecimalFormat进行数字格式化import java.text.DecimalFormat;public class Demo7 public static void main(String args) int num1=1234567;double num2=0.126543;DecimalFormat df1=new DecimalFormat(#,#); DecimalFormat df2=new DecimalFormat(#.00); DecimalFormat df3=new DecimalFormat(00.#); DecimalFormat df4=new DecimalFormat(0.#E0); DecimalFormat df5=new DecimalFormat(0.#%); System.out.println(df1.format(num1);System.out.println(df2.format(num2);System.out.println(df3.format(num2);System.out.println(df4.format(num1);System.out.println(df5.format(num2);代码解析: #:代表一个位置数字,如果该位置数字不存在,则省略不显示。,:代表数字中的分隔符,此示例用三位分隔一次。 0:代表一个数字位置,如果该位置不存在,则用0来补充。小数中多余部分四舍五入。.:表示小数点。#:当前位置是0,则省略不显示。 #:小数部分只显示1位小数,并且进行四舍五入。 E:科学计数法。 %:用百分数表示数字。代码输出结果:1,234,567.1300.11.23E612.65%4. java.util.CalendarCalendar类是一个抽象类,它为特定的值诸如YEAR、MONTH、DAY_OF_MONTH、HOUR等日历字段之间的转换和操作日历字段(例如获得下星期的日期)提供了丰富的方法。并且可以非常方便的与Date类型进行相互转换。使用静态方法getInstance()和getInstance(Locale locale)获取Calendar对象。Calendar定义了很多表示日期时间中各个部分的常量字段。返回值字段说明static intAM指示从午夜到中午之前这段时间的 AM_PM 字段值。static intDATEget 和 set 的字段,指示一个月中的某天。static intDAY_OF_MONTHget 和 set 的字段,指示一个月中的某天。static intDAY_OF_WEEKget 和 set 的字段,指示一个星期中的某天。static intDAY_OF_YEARget 和 set 的字段,指示当前年中的天数。static intHOURget 和 set 的字段,指示上午或下午的小时。static intHOUR_OF_DAYget 和 set 的字段,指示一天中的小时。static intMINUTEget 和 set 的字段,指示一小时中的分钟。static intMONTH指示月份的 get 和 set 的字段。static intPM指示从中午到午夜之前这段时间的 AM_PM 字段值。static intSECONDget 和 set 的字段,指示一分钟中的秒。static intWEEK_OF_MONTHget 和 set 的字段,指示当前月中的星期数。static intWEEK_OF_YEARget 和 set 的字段,指示当前年中的星期数。static intYEAR表示年的 get 和 set 的字段。表9 Calendar类中的日期字段Calendar类提供了丰富的操作方法,可以单独对年、月、日、时、分、秒等字段单独读取,也可以对星期设置,常用方法如下:返回方法说明voidadd(int field, int amount)根据日历的规则,为给定的日历字段添加或减去指定的时间量。booleanafter(Object when)判断此 Calendar 表示的时间是否在指定 Object 表示的时间之后,返回判断结果。booleanbefore(Object when)判断此 Calendar 表示的时间是否在指定 Object 表示的时间之前,返回判断结果。intget(int field)返回给定日历字段的值。intgetActualMaximum(int field)给定此 Calendar 的时间值,返回指定日历字段可能拥有的最大值。intgetActualMinimum(int field)给定此 Calendar 的时间值,返回指定日历字段可能拥有的最小值。DategetTime()返回一个表示此 Calendar 时间值(从历元至现在的毫秒偏移量)的 Date 对象。longgetTimeInMillis()返回此 Calendar 的时间值,以毫秒为单位。voidset(intfield, intvalue)将给定的日历字段设置为给定值。voidset(int year, int month, int date)设置日历字段 YEAR、MONTH 和 DAY_OF_MONTH 的值。voidset(int year, int month, int date, int hourOfDay, int minute)设置日历字段 YEAR、MONTH、DAY_OF_MONTH、HOUR_OF_DAY 和 MINUTE 的值。voidset(int year, int month, int date, int hourOfDay, int minute, int second)设置字段 YEAR、MONTH、DAY_OF_MONTH、HOUR、MINUTE 和 SECOND 的值。voidsetTime(Date date)使用给定的 Date 设置此 Calendar 的时间。voidsetTimeInMillis(long millis)用给定的 long 值设置此 Calendar 的当前时间值。表10 Calendar类常用方法代码演示:Calendar的使用import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Locale;public class Demo8 public static void main(String args) SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);Calendar cale = Calendar.getInstance();cale.set(2009, 8, 20);/ 年月日同时设置 cale.set(Calendar.DAY_OF_WEEK, 2); Date date1 = cale.getTime(); System.out.println(sdf.format(date1); cale.set(Calendar.MONTH, 3); cale.set(Calendar.DAY_OF_MONTH, 28); cale.set(Calendar.YEAR, 1978); Date date2 = cale.getTime();System.out.println(sdf.format(date2);代码解析: 可以使用set方法对年月日时分秒同时设置。 把天定位到星期一,Calendar中认为第一天是星期天,设置2就是星期一。 Calendar类型转换为日期时间等价的Date类型。 单独设置月。 单独设置日。 单独设置年。代码输出结果:2009-09-21 17:21:371978-04-28 17:21:37Q 老师,为什么通过Calendar设置月与输出差1个月?A 不是差一个月,而是在Calendar中对月份的计算是从0开始的,因此设置月份11其实就是中国的十二月。5. Java对集合的操作Java中学习了集合的操作,比如:排序、搜索等,Java中用java.util.Arrays对数组操作,使用java.util.Collections对集合框架中List操作。他们都是工具类,类中的方法全部都是静态方法。 Arrays中的方法1. void Arrays.sort(T) 对数组中的元素按照升序进行排序。T代表某一数据类型。代码演示:binarySearch使用public static void main(String args) int arrs=new int12,54,12,8765,123,34,54,23,67;Arrays.sort(arrs);for (int i : arrs) System.out.print(i+ );代码输出结果:12 12 23 34 54 54 67 123 8765 在sort方法中,遇到对象数组的排序时,要给对象提供排序的依据,实现Comparator接口,可以在接口的compare方法中指定排序规则,实现Comparator接口的对象称为比较器。有一个Student类的数组,现在按照年龄进行升序排序,那么Comparator接口compare方法实现如下:代码演示:compare重新按年龄实现class Student String name;int age;public Student(String name, int age) super(); = name;this.age = age; public String toString() return name + , + age;class StuCom implements Comparator public int compare(Student stu1, Student stu2) if (stu1.age stu2.age) return 1; else if (stu1.age = stu2.age) return 0; else return -1;public static void main(String args) Student stus = new Student new Student(小美, 21),new Student(阿聪, 22), new Student(武大郎, 28),new Student(阮小七, 26), new Student(晁盖, 30),new Student(鲁智深, 29), new Student(孙二娘, 26),new Student(扈三娘, 23), new Student(武松, 24) ;Arrays.sort(stus, new StuCom();for (Student student : stus) System.out.println(student);代码解析: 定义一个比较器,必须实现Comparator接口,否则系统无法对一个对象数组进行搜索规则。 实现Comparator接口的compare方法,对该方法中的两个参数进行比较,就是制定了比较的规则。代码输出结果:小美,21阿聪,22扈三娘,23武松,24阮小七,26孙二娘,26武大郎,28鲁智深,29晁盖,302. List Arrays.asList(Object objs) 把指定的数组转换为List的对象。代码演示:asList使用import java.util.Arrays;import java.util.List;public class Demo9 public static void main(String args) String strs=aaa,bbb,ccc,ddd,eee,fff,ggg,hhh,iii,jjj;List list=Arrays.asList(strs);for (int i = 0; i list.size(); i+) System.out.println(list.get(i);3. int Arrays.binarySearch(T objs, key) 在数组objs中查找key的位置,返回key的下标,如果查找不到key,则返回负值。int Arrays.binarySearch(T objs,int fromIndex,int toIndex , key)在数组objs中从fromIndex到toIndex位置上查找key,返回key的下标,如果查找不到,返回一个负值。在binarySearch方法调用之前一定要保证数组已经是排序的,如果没有排序,可以使用Arrays.sort(T) 进行排序,然后再进行查找。代码演示:binarySearch使用public static void main(String args) String strs=aaa,bbb,ccc,ddd,eee,fff,ggg,hhh,iii,jjj;Arrays.sort(strs);System.out.println(Arrays.binarySearch(strs, ccc);System.out.println(Arrays.binarySearch(strs, 4,8,ggg);System.out.println(Arrays.binarySearch(strs, 4,8,aaa);如果数组是一个自定义的对象数组,那么搜索之前要先指定比较器。代码演示:binarySearch搜索对象使用class StuCom implements Comparator public int compare(Student stu1, Student stu2) if (stu1.age stu2.age) return 1; else if (stu1.age = stu2.age & .equals() return 0; else return -1;public static void main(String args) Student stus = new Student new Student(小美, 21),new Student(阿聪, 22), new Student(武大郎, 28),new Student(阮小七, 26), new Student(晁盖, 30),new Student(鲁智深, 29), new Student(孙二娘, 26),new Student(扈三娘, 23), new Student(武松, 24) ;Student s = new Student(晁盖, 30);System.out.println(Arrays.binarySearch(stus, s, new StuCom(); 代码解析: 该比较器规定了要比较的类型就是Student类型,因此这里使用泛型。 指定了对象数组,对象和比较器的方法进行搜索。结果返回搜索到的对象在数组中的下标。除了上面介绍Arrays的方法外,还有一些其它的方法:方法说明T copyOf(T t,int length)把一个数组赋值到长度是length的新数组中。T表示数据类型。fill(T t,N newValue)用一个固定值填充数组中所有元素。表11 Arrays其他常用方法。 Collections类Collections类与Arrays类一样都提供了一系列的静态方法,只是Arrays主要操作数组,而Collections主要操作List集合,同时还有对Set的相关操作。代码演示:Collections操作import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Demo10 static class Student implements Comparable String name;int age;public Student(String name, int age) super(); = name;this.age = age;public String toString() return name + , + age;public int compareTo(Object o) Student stu = (Student) o;if (this.age stu.age) return 1; else if (this.age = stu.age & .equals() return 0; else return -1;public static void main(String args) List list = new ArrayList();Student stus = new Student new Student(小美, 21),new Student(阿聪, 22), new Student(武大郎, 28),new Student(阮小七, 26), new Student(晁盖, 30),new Student(鲁智深, 29), new Student(孙二娘, 26),new Student(扈三娘, 23), new Student(武松, 24) ;Collections.addAll(list, stus); Collections.sort(list); for (Student student : stus) System.out.println(student);Student stu = new Student(鲁智深, 29);int pos = Collections.binarySearch(list, stu); System.out.println(pos)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论