常用类
1.包装类 1.针对八种基本数据类型相应的引用类型-包装类 2.有了类的特点,就可以调用类中的方法
基本数据类型
包装类
boolean
Boolean
char
Character
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
案例:演示JDK5以前的手动装箱和拆箱
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class Integer01 { public static void main (String[]args) { int n1 = 100 ; Integer integer = new Integer (n1); Integer integer1 = Integer.valueof(n1); int i = integer.intValue(); int n2 = 200 ; Integer integer2 = n2; int n3 = integer2; } }
1.2包装类方法 案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class WrapperVSString { public static void main (String[]args) { Integer i = 100 ; String str1 = i+"" ; String str2 = i.toString(); String str3 = String.valueOf(i); String str4 = "12345" ; Integer i2 = Integer.parseInt(str4); Integer i3 = new Integer (str4); } }
1.3包装类Integer和Character类常用方法 1 2 3 4 5 6 7 8 9 System.out.println(Integer.MIN_VALUE) System.out.println(Integer.MAX_VALUE) System.out.println(Character.isDigit('a' )); System.out.println(Character.isLetter('a' )); System.out.println(Character.isUpperCase('a' )) System.out.println(Character.isLowerCase('a' )) System.out.println(Character.isWhitespace('a' )) System.out.println(Character.toUpperCace('a' )) System.out.println(Character.toLowerCace('A' ))
1.4String类 1.String 对象用于保存字符串,也就是一组字符序列,双引号括起来的 2.String常见的构造器 2.1 String s1 = new String(); 2.2 String s2 = new String(String orininal); 2.3 String s3 = new String(char[] a); 2.4 String s4 = new String(char[] a,int strartIndex,int count); 3.String 有属性 private final char value[];用于存放字符串内容 3.1注意:value是一个final类型,不可以将value指向新的地址,但是单个字符的内容可以变化。
1.4.1String对象的创建方式 方式一:直接赋值String s = “DADADA”; 方式二:调用构造器String s = new String(“DADADAD”);
1.4.2String对象的不可变性 任何一个String对象在创建之后都不能对它的内容作出任何改变。对于连接、获得子串和改变大小写等操作,如果返回值同原字符串不同,实际上是产生了一个新的String对象,在程序的任何地方,相同的字符串字面常量都是同一个对象,下面的代码会改变字符串s的内容吗? String s = “Java”; s = “HTML”; 答案是不会。第一条语句创建了一个内容为”Java”的String对象,并将其引用赋值给s。第二条语句创建了一个内容为”HTML”的新String对象,并将其引用赋值给s。赋值后第一个String对象仍然存在,但是不能再访问它,因为变量s现在指向了新的对象 案例:
1 2 3 4 5 6 7 8 String str1 = "hello" ; String str2 = new String ("hello" );String str3 = "hello" ; System.out.println(str1==str2); System.out.println(str1==str3);false true
1.5String的常用方法 1.equals //区分大小写 2.equalslgnoreCase //忽略大小写判断内容是否相等 3.length //获取字符的个数,字符串长度 4.indexOf//获取字符在字符串中第一次出现的索引,从0开始找不到返回-1 5.lastindexOf//获取字符串在字符中最后一次出现的索引,从0开始,找不到返回-1 6.substring//截取指定范围的字符串 7.trim //去前后空格 8.charAt//获取某索引处的字符,不能使用charAt[index]这种方式 9.toUpperCase //转换成大写 10.toLowerCase //转换成小写 11.concat //拼接字符串 12.replace //替换字符串中的字符 13.split //分割字符串,对于某些分割字符,我们需要转义比如|、\\等 例子:
1 2 3 String poem = "锄禾日当午,汗滴禾下土,谁之盘中餐,粒粒皆辛苦" String[ ]split = poem.split("," );
14.compate To //比较两个字符串的大小 15.toCharArray//转换成字符数组 16.format //格式化字符串,%s字符串,%c字符,%d整形,%.2f浮点型,类似c语言
1.6 StringBuffer类 1.java.long.StringBuffer代表可变的字符序列,可以对字符串内容进行删减 2.很多方法与String相同,但StringBuffer是可变长度的 3.StringBuffer是一个容器 使用方法:
1 2 3 4 StringBuffer s1 = new StringBuffer ("hello" );
4.String和StringBuffer的对比 4.1String保存的是字符串常量,里面的值不能更改,每次String类的更新实际上就是更改地址,效率比较低//private final char value []; 放在常量池 4.2StringBuffer保存的是字符串变量,里面的值可以更改,每次StringBuffer的更新实际上可以更新内容,不用更新地址,效率较高//char[] value 这个放在堆
1.6.1StringBuffer的构造器 1.StringBuffer() //构造一个其中不带字符的字符串缓冲区,其初始容量为16个字符 使用方法:
1 StringBuffer s1 = new StringBuffer ();
2.StringBuffer(CharSequence seq) //构造一个字符串缓冲区,它包含与指定的CharSequence相同的字符 3.StringBuffer(int capacity) //capacity[容量],构造一个不带字符,但是具有指定初始容量的字符串缓冲区。即对char[]大小进行指定 使用方法:
1 StringBuffer s1 = new StringBuffer (100 );
4.StringBuffer(string str) //构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容
1 2 StringBuffer s1 = new StringBuffer ("hello" );
1.6.2String和StringBuffer相互转换 案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 String s = "hello" ;StringBuffer b1 = new StringBuffer (s);StringBuffer b2 = new StringBuffer (); b2=b2.append(s);StringBuffer b1 = new StringBuffer ("haozi" );String s2 = b1.toString();String s3 = new String (b1);
1.6.3StringBuffer类常见方法 1.append //增 2.delete(start,end) 3.replace(start,end,string)//将start–end间的内容替换掉,不含end 4.indexOf //查找子串在字符串第一次出现的索引,如果找不到返回-1 5.insert //插 6.length //获取长度 案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 StringBuffer s = new StringBuffer ("hello" ); s.append(',' ); s.append("张三丰" ); s.append("赵敏" ).append(100 ).append(true ).append(10.5 ); System.out.println(s); s.delete(11 ,14 ); System.out.println(s); s.replace(9 ,11 ,"haozi" ); System.out.println(s); s.insert(9 ,"赵敏" ); System.out.println(s); System.out.println(s.length());
例题1:
1 2 3 4 5 6 7 8 String str = null ;StringBuffer sb = new StringBuffer (); sb.append(str); System.out.println(sb.length()); System.out.println(sb);StringBuffer sb1 = new StringBuffer (str); System.out.println(sb1);
例题2:
1 2 3 4 5 6 7 String price = "123564.59" ;StringBuffer sb = new StringBuffer (price);for (int i = sb.lastIndexOf("." )-3 ;i>0 ;i=i-3 ){ sb = sb.insert(i,"," ); } System.out.println(sb);
1.7StringBuilder类 1.一个可变的字符序列,此类提供一个与StringBuffer兼容的API,该类被设计作用StringBuffer的一个简易替换,用在字符串缓冲区被单个线程使用的时候。如果可能,建议优先采用该类,因为在大多数实现中,比StringBuffer要块 2.StringBuilder上主要的操作还是append和insert方法,可重载这些方法以接受任意类型的数据
1.7.1 String、StringBuffer、StringBuilder的比较 1.StringBuilder和StringBuffer非常类似,均代表可变的字符序列,而且方法一样 2.String:不可变字符序列,效率低,但是复用性高(多个对象指向一个常量池) 3.StringBuffer:可变字符序列,效率较高(增删),多线程 4.StringBuilder:可变字符序列,效率最高,单线程
1.8Math类 概念:数学的运算方法 方法:
方法名
描述
abs(double a)
返回double的绝对值
acos(double a)
返回一个值的反余弦
asin(double a)
返回一个值的反正弦
atan(double a)
返回一个值的反正切
cbrt(double a)
返回double值得立方根
pow(int a)
求幂
ceil()
向上取整
floor()
向下取整
sqrt(int a)
求开方
max(int a,int b)
求两个数的最大值
min(int a,int b)
求两个数的最小数
注意random生成随机数的使用方法是:
1 2 3 4 5 6 7 Random r = new Random (); System.out.println(r.nextInt(100 ));
1.9Arrays类 概念:Arrays类中包含了一系列的静态方法,用于管理或操作数组(排序或搜索) 1.toString 返回数组的字符串格式 用法:Arrays.toString(arr) 2.sort排序(自然排序和定制排序)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Integer arr[] = {1 ,-1 ,7 ,0 ,89 }; Arrays.sort(arr); System.out.println(Arrays.toString(arr)); Arrays.sort(arr,new Comparator (){ public int compare (Object o1,Object o2) { Integer i1 = (Integer) o1; Integer i2 = (Integer) o2; return i2-i1 } });
案例根据书的金额进行排序:
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 package Arrays;import java.util.Arrays;import java.util.Comparator;public class work1 { public static void main (String[]args) { book[] books = new book [4 ]; books[0 ] = new book ("红楼梦" ,100 ); books[1 ] = new book ("金瓶梅新" ,90 ); books[2 ] = new book ("青年文摘20年" ,5 ); books[3 ] = new book ("java从入门到放弃" ,300 ); Arrays.sort(books,new Comparator () { public int compare (Object o1,Object o2) { book book1 = (book)o1; book book2 = (book)o2; double priceVal = book2.getPrice()-book1.getPrice(); if (priceVal > 0 ) { return 1 ; }else if (priceVal < 0 ) { return -1 ; }else { return 0 ; } } }); System.out.println(Arrays.toString(books)); } }class book { String name; double price; public book (String name,double price) { this .name = name; this .price = price; } public String getName () { return name; } public void setName (String name) { this .name = name; } public double getPrice () { return price; } public void setPrice (double price) { this .price = price; } @Override public String toString () { return "book [name=" + name + ", price=" + price + "]" ; } }
3.binarySearch 通过二分搜索法进行查找,要求必须排好序
1 2 3 4 5 6 Integer[] arr = {1 ,2 ,90 ,123 ,567 }int index = Arrays.binarySearch(arr,1 ); System.out.println(index);
4.copyOf数组元素的复制
1 2 Integer[] newArr = Arrays.copyOf(arr,arr.length)
5.fill数组元素的填充
1 2 3 4 Integer[] num = new Integer []{9 ,3 ,2 }; Arrays.fill(num,99 ); System.out.println(Arrays.toString(num));
6.equals比较两个数组内容是否一致
1 2 3 4 5 Integer[] arr = {1 ,2 ,90 ,123 ,567 } Integer[] arr2 = {1 ,2 ,90 ,123 ,567 };boolean equals = Arrays.equals(arr,arr2) System.out.println(equals)
7.asList将一组值,转换成list集合
1 2 3 List<Integer> asList = Arrays.asList(2 ,3 ,4 ,5 ,6 ,1 ) System.out.println(asList);
1.10System类 1.exit退出当前程序
1 2 3 4 System.out.println("ok1" ); System.exit(0 );
2.arraycopy:复制数组元素,比较适合底层使用
1 2 3 4 int [] src = {1 ,2 ,3 }int [] dest = new int [3 ]; System.out.arraycopy(src,0 ,dest,0 ,3 );
3.currentTimeMillens:返回当前时间距离1970-1-1的毫秒数
1 System.out.println(System.currentTimeMillens);
4.gc:运行垃圾回收机制System.gc(); 5.Biginteger和BigDecimal类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 long l = 222222222222222222222222l ; BigInteger biginteger = new BigInteger ("222222222222222222222222" );BigInteger biginteger2 = new BigInteger ("22" );BigInteger add = bigInteger.add(bigInteger2);BigInteger subtract = bigInteger.add(bigInteger2);BigInteger multiply = bigInteger.add(bigInteger2);BigInteger divide = bigInteger.add(bigInteger2);BigDecimal bigDecimal1 = new BigDecimal ("1999.1215165156156165" );
1.10DATE日期类 1.11案例 1.将字符串指定区域反转
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 package Arrays;public class work2 { public static void main (String[]args) { String s1 = "abcdef" ; s1 = reverse(s1,1 ,4 ); System.out.println(s1); } public static String reverse (String s1,int start ,int end) { if (s1 !=null && start >=0 && end > start && end < s1.length() ) { throw new RuntimeException ("参数不正确" ); } char [] chars = s1.toCharArray(); char temp = ' ' ; for (int i = start,j=end ;i<j;i++,j--) { temp = chars[i]; chars[i] = chars[j]; chars[j] = temp; } String chars2 = new String (chars); return chars2; } }
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 public class work3 { public static void main (String[]args) { String name = "jack" ; String pwd = "123456" ; String email = "haozi@qq.com" ; userRegister(name,pwd,email); } public static void userRegister (String name,String passwd,String email) { int namelength = name.length(); int q = 1 ; int w = 1 ; int e = 1 ; int r = 1 ; if (!(namelength>=2 && namelength<=4 )){ System.out.println("用户名错误" ); q=0 ; } char [] char1 = passwd.toCharArray(); if (passwd.length()!=6 ) { System.out.println("密码长度错误" ); w=0 ; } for (int i = 0 ;i<char1.length;i++) { if (char1[i] < '0' || char1[i] >'9' ) { System.out.println("密码格式不正确" ); e=0 ; break ; } } int x = email.indexOf('@' ); int y = email.indexOf('.' ); if (!(x> 0 && y>x)) { System.out.println("邮箱格式有错" ); r=0 ; } if (q==1 && w==1 && e==1 && r==1 ) { System.out.println("注册成功" ); } } }
3.字符串统计
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 package Arrays;import java.util.*;public class work4 { public static void main (String[]args) { Scanner s1 = new Scanner (System.in); String s2 = s1.nextLine(); String str1 = "li hao zi" ; name(s2); } public static void name (String str) { if (str == null ) { System.out.println("输入的字符串不能为空" ); return ; } String[ ] names = str.split(" " ); if (names.length!=3 ) { System.out.println("输入的字符串格式错误" ); return ; } String format = String.format("%s,%s .%c" , names[2 ],names[0 ],names[1 ].toUpperCase().charAt(0 )); System.out.println(format); } }
4.输入一个字符串,统计字符串中的大写字母、小写字母、数字个数
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 public class work5 { public static void main (String[]args) { Scanner s1 = new Scanner (System.in); String s2 = s1.nextLine(); name(s2); } public static void name (String str1) { int sz=0 ; int zm=0 ; int dxzm=0 ; char [] str2 = str1.toCharArray(); for (int i = 0 ;i<str2.length;i++) { if (str2[i]>='A' &&str2[i]<='Z' ) { dxzm = dxzm + 1 ; }else if (str2[i]>='a' &&str2[i]<='z' ) { zm = zm + 1 ; }else if (str2[i]>='0' &&str2[i]<='9' ) { sz = sz + 1 ; } } System.out.println(dxzm); System.out.println(zm); System.out.println(sz); } }