Arrays Arrays 是针对数组进行操作的工具类。
常用成员方法 1 2 3 public static String toString (int [] a) public static void sort (int [] a) public static int binarySearch (int [] a,int key)
其中,toString() 源码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public static String toString (int [] a) { if (a == null ) return "null" ; int iMax = a.length - 1 ; if (iMax == -1 ) return "[]" ; StringBuilder b = new StringBuilder (); b.append('[' ); for (int i = 0 ; ; i++) { b.append(a[i]); if (i == iMax) return b.append(']' ).toString(); b.append(", " ); } }
binarySearch() 调用的是 binarySearch0(),binarySearch0() 源码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 private static int binarySearch0 (int [] a, int fromIndex, int toIndex,int key) { int low = fromIndex; int high = toIndex - 1 ; while (low <= high) { int mid = (low + high) >>> 1 ; int midVal = a[mid]; if (midVal < key) low = mid + 1 ; else if (midVal > key) high = mid - 1 ; else return mid; } return -(low + 1 ); }
使用示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class ArraysDemo { public static void main (String[] args) { int [] arr = { 24 , 69 , 80 , 57 , 13 }; System.out.println("排序前:" + Arrays.toString(arr)); Arrays.sort(arr); System.out.println("排序后:" + Arrays.toString(arr)); System.out.println("binarySearch:" + Arrays.binarySearch(arr, 57 )); System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577 )); } }
BigDemical BigDecimal类:不可变的、任意精度的有符号十进制数,可以解决数据丢失问题。
看如下程序,写出结果:
1 2 3 4 5 6 7 public static void main (String[] args) { System.out.println(0.09 + 0.01 ); System.out.println(1.0 - 0.32 ); System.out.println(1.015 * 100 ); System.out.println(1.301 / 100 ); System.out.println(1.0 - 0.12 ); }
结果和我们想的有一点点不一样,这是因为浮点数类型的数据存储和整数不一样导致的。 它们大部分的时候,都是带有有效数字位。由于在运算的时候,float类型和double很容易丢失精度, 所以,为了能精确的表示、计算浮点数,Java 提供了 BigDecimal。
常用成员方法 1 2 3 4 5 6 7 8 9 10 11 12 public BigDecimal (String val) public BigDecimal add (BigDecimal augend) public BigDecimal subtract (BigDecimal subtrahend) public BigDecimal multiply (BigDecimal multiplicand) public BigDecimal divide (BigDecimal divisor) public BigDecimal divide (BigDecimal divisor,int scale,int roundingMode)
使用示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 public static void main (String[] args) { BigDecimal bd1 = new BigDecimal ("0.09" ); BigDecimal bd2 = new BigDecimal ("0.01" ); System.out.println("add:" + bd1.add(bd2)); System.out.println("-------------------" ); BigDecimal bd3 = new BigDecimal ("1.0" ); BigDecimal bd4 = new BigDecimal ("0.32" ); System.out.println("subtract:" + bd3.subtract(bd4)); System.out.println("-------------------" ); BigDecimal bd5 = new BigDecimal ("1.015" ); BigDecimal bd6 = new BigDecimal ("100" ); System.out.println("multiply:" + bd5.multiply(bd6)); System.out.println("-------------------" ); BigDecimal bd7 = new BigDecimal ("1.301" ); BigDecimal bd8 = new BigDecimal ("100" ); System.out.println("divide:" + bd7.divide(bd8)); System.out.println("divide:" + bd7.divide(bd8, 3 , BigDecimal.ROUND_HALF_UP)); System.out.println("divide:" + bd7.divide(bd8, 8 , BigDecimal.ROUND_HALF_UP)); }
BigInteger BigInteger:可以让超过 Integer 范围内的数据进行运算。
常用成员方法 1 2 3 4 5 6 7 8 9 public BigInteger add (BigInteger val) public BigInteger subtract (BigInteger val) public BigInteger multiply (BigInteger val) public BigInteger divide (BigInteger val) public BigInteger[] divideAndRemainder(BigInteger val)
使用示例 示例1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class BigIntegerDemo { public static void main (String[] args) { Integer num = new Integer ("2147483647" ); System.out.println(num); BigInteger num2 = new BigInteger ("2147483648" ); System.out.println(num2); } }
示例2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class BigIntegerDemo2 { public static void main (String[] args) { BigInteger bi1 = new BigInteger ("100" ); BigInteger bi2 = new BigInteger ("50" ); System.out.println("add:" + bi1.add(bi2)); System.out.println("subtract:" + bi1.subtract(bi2)); System.out.println("multiply:" + bi1.multiply(bi2)); System.out.println("divide:" + bi1.divide(bi2)); BigInteger[] bis = bi1.divideAndRemainder(bi2); System.out.println("divide:" + bis[0 ]); System.out.println("remainder:" + bis[1 ]); } }
Character Character 类在对象中包装一个基本类型 char 的值.此外,该类提供了几种方法, 以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然。
常用成员方法 1 2 3 4 5 6 7 8 9 10 11 Character(char value) public static boolean isUpperCase (char ch) public static boolean isLowerCase (char ch) public static boolean isDigit (char ch) public static char toUpperCase (char ch) public static char toLowerCase (char ch)
使用示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class CharacterDemo { public static void main (String[] args) { System.out.println("isUpperCase:" + Character.isUpperCase('A' )); System.out.println("isUpperCase:" + Character.isUpperCase('a' )); System.out.println("isUpperCase:" + Character.isUpperCase('0' )); System.out.println("-----------------------------------------" ); System.out.println("isLowerCase:" + Character.isLowerCase('A' )); System.out.println("isLowerCase:" + Character.isLowerCase('a' )); System.out.println("isLowerCase:" + Character.isLowerCase('0' )); System.out.println("-----------------------------------------" ); System.out.println("isDigit:" + Character.isDigit('A' )); System.out.println("isDigit:" + Character.isDigit('a' )); System.out.println("isDigit:" + Character.isDigit('0' )); System.out.println("-----------------------------------------" ); System.out.println("toUpperCase:" + Character.toUpperCase('A' )); System.out.println("toUpperCase:" + Character.toUpperCase('a' )); System.out.println("-----------------------------------------" ); System.out.println("toLowerCase:" + Character.toLowerCase('A' )); System.out.println("toLowerCase:" + Character.toLowerCase('a' )); } }
练习 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 public class CharacterTest { public static void main (String[] args) { Scanner sc=new Scanner (System.in); String str=sc.nextLine(); printCount(str); printCount2(str); } public static void printCount (String str) { int numberCount=0 ; int lowercaseCount=0 ; int upercaseCount=0 ; for (int index=0 ;index<str.length();index++){ char ch=str.charAt(index); if (ch>='0' && ch<='9' ){ numberCount++; }else if (ch>='A' && ch<='Z' ){ upercaseCount++; }else if (ch>='a' && ch<='z' ){ lowercaseCount++; } } System.out.println("数字有" +numberCount+"个" ); System.out.println("小写字母有" +lowercaseCount+"个" ); System.out.println("大写字母有" +upercaseCount+"个" ); } public static void printCount2 (String str) { int numberCount=0 ; int lowercaseCount=0 ; int upercaseCount=0 ; for (int index=0 ;index<str.length();index++){ char ch=str.charAt(index); if (Character.isDigit(ch)){ numberCount++; }else if (Character.isUpperCase(ch)){ upercaseCount++; }else if (Character.isLowerCase(ch)){ lowercaseCount++; } } System.out.println("数字有" +numberCount+"个" ); System.out.println("小写字母有" +lowercaseCount+"个" ); System.out.println("大写字母有" +upercaseCount+"个" ); } }
Scanner Scanner:用于接收键盘录入数据。使用 Scanner 三部曲:导包、创建对象、使用相应方法。
常用成员方法 1 2 3 4 5 6 Scanner(InputStream source) public boolean hasNextXxx () public Xxx nextXxx ()
使用示例 示例1:
1 2 3 4 5 6 7 8 9 10 11 public class ScannerDemo { public static void main (String[] args) { Scanner sc=new Scanner (System.in); if (sc.hasNextInt()){ int x=sc.nextInt(); System.out.println("x=" +x); }else { System.out.println("输入的数据有误" ); } } }
示例2:先获取一个数值,换行后,再获取一个字符串,会出现问题。主要原因:就是换行符号的问题。如何解决呢?
1 2 3 4 5 6 7 8 9 10 public static void main (String[] args) { Scanner sc=new Scanner (System.in); int x=sc.nextInt(); System.out.println("x:" +x); String line=sc.nextLine(); System.out.println("line:" +line); }
解决方案一:先获取一个数值后,再创建一个新的键盘录入对象获取字符串。
1 2 3 4 5 6 7 8 9 public static void method () { Scanner sc=new Scanner (System.in); int x=sc.nextInt(); System.out.println("x:" +x); Scanner sc2=new Scanner (System.in); String line=sc2.nextLine(); System.out.println("line:" +line); }
解决方案二:把所有的数据都先按照字符串获取,然后要什么,就进行相应的转换。
1 2 3 4 5 6 7 8 9 10 11 public static void method2 () { Scanner sc=new Scanner (System.in); String xStr=sc.nextLine(); String line=sc.nextLine(); int x=Integer.parseInt(xStr); System.out.println("x:" +x); System.out.println("line:" +line); }
Calendar Calendar为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法, 并为操作日历字段(例如获得下星期的日期)提供了一些方法。
常用成员方法 1 2 3 4 5 6 7 8 public int get (int field) public void add (int field,int amount) public final void set (int year,int month,int date)
使用示例 示例1:
1 2 3 4 5 6 7 8 9 10 11 public class CalendarDemo { public static void main (String[] args) { Calendar rightNow = Calendar.getInstance(); int year=rightNow.get(Calendar.YEAR); int month=rightNow.get(Calendar.MONTH); int date=rightNow.get(Calendar.DATE); System.out.println(year + "年" + (month + 1 ) + "月" + date + "日" ); } }
示例2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class CalendarDemo2 { public static void main (String[] args) { Calendar calendar = Calendar.getInstance(); System.out.println(getYearMonthDay(calendar)); calendar.add(Calendar.YEAR,-3 ); System.out.println(getYearMonthDay(calendar)); calendar.add(Calendar.YEAR,5 ); calendar.add(Calendar.DATE,-10 ); System.out.println(getYearMonthDay(calendar)); calendar.set(2011 ,10 ,11 ); System.out.println(getYearMonthDay(calendar)); } public static String getYearMonthDay (Calendar calendar) { int year=calendar.get(Calendar.YEAR); int month=calendar.get(Calendar.MONTH); int date=calendar.get(Calendar.DATE); return year + "年" + (month + 1 ) + "月" + date + "日" ; } }
练习 获取任意一年的二月有多少天
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class CalendarTest { public static void main (String[] args) { Scanner sc=new Scanner (System.in); int year=sc.nextInt(); Calendar c= Calendar.getInstance(); c.set(year,2 ,1 ); c.add(Calendar.DATE,-1 ); System.out.println(year+"年,二月有" +c.get(Calendar.DATE)+"天" ); } }
Date Date: 表示特定的瞬间,精确到毫秒。
常用成员方法 1 2 3 4 5 6 7 Date() Date(long date) public long getTime () public void setTime (long time)
使用示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public class DateDemo { public static void main (String[] args) { Date d = new Date (); System.out.println("d:" + d); long time = 1000 * 60 * 60 ; Date d2 = new Date (time); System.out.println("d2:" + d2); long time2 = d.getTime(); System.out.println(time2); System.out.println(System.currentTimeMillis()); d.setTime(1000 *60 *60 ); System.out.println("d:" + d); } }
Date 和 String 类型的相互转换 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public class DateFormatDemo { public static void main (String[] args) { Date date=new Date (); SimpleDateFormat sdf=new SimpleDateFormat ("yyyy年MM月dd日" ); String s=dateToString(date,sdf); System.out.println(s); System.out.println(stringToDate(s,sdf)); } public static String dateToString (Date d, SimpleDateFormat sdf) { return sdf.format(d); } public static Date stringToDate (String s, SimpleDateFormat sdf) { Date date=null ; try { date=sdf.parse(s); } catch (ParseException e) { e.printStackTrace(); } return date; } }
DateForamt:可以进行日期和字符串的格式化和解析,但是由于是抽象类,所以使用具体子类SimpleDateFormat。
1 2 3 SimpleDateFormat() SimpleDateFormat(String pattern)
这个模式字符串该如何写呢? 通过查看 API,我们就找到了对应的模式:
中文说明
模式字符
年
y
月
M
日
d
时
H
分
m
秒
s
练习 算一下你来到这个世界多少天?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 小练习: public class DateTest { public static void main (String[] args) throws ParseException { Scanner sc = new Scanner (System.in); System.out.println("请输入你的出生年月日(格式 yyyy-MM-dd):" ); String line = sc.nextLine(); SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd" ); Date d = sdf.parse(line); long birth=d.getTime(); long current=System.currentTimeMillis(); long days=(current-birth)/1000 /60 /60 /24 ; System.out.println("你出生了" +days+"天" ); } }