11异常

1.1抛出

在代码可能出现异常的地方进行抛出异常

使用关键字:

  1. throw:在方法中抛出异常对象,自己造一个异常!!!
  2. shrows:在方法的声明位置去书写,告知调用者,当前抛出的异常有哪些。

编译时异常需要在方法名“后”加 throws,如果只是执行时异常,在方法“中”加throw

1.2自定义异常

自己建一个类,去继承Exception

  1. package com.qfedu.test1;
  2. //自定义异常
  3. public class Demo4 {
  4. public static void main(String[] args) throws SingleDogException {
  5. buy(false);
  6. buy(true);
  7. }
  8. public static void buy(boolean isSingle) throws SingleDogException {
  9. if (isSingle) {
  10. throw new SingleDogException("单身狗不能进店");
  11. }
  12. System.out.println("情侣买一送一");
  13. }
  14. }
  15. //自己定义一个异常继承父类
  16. class SingleDogException extends Exception{
  17. public SingleDogException() {}
  18. public SingleDogException(String message) {
  19. super(message);//调用父类的有参构造
  20. }
  21. }

throws是抛出,不管异常,运行报红。try catch还管管你

2.String 类

想要学习一个类:用api搜String,看类的关系和类下面的构造方法和方法

    • String(String original) 初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本。

2.1判断两个字符串是否相同

Demo1

==: 比较的是内存地址和内容(严格)

    • boolean equals(Object anObject) 将此字符串与指定对象进行比较。

equals:a.equlas(“b”) 只比较内容(不严格)

以后开发中比较字符串,必须使用equlas

  1. package com.qfedu.test2;
  2. public class Demo1 {
  3. public static void main(String[] args) {
  4. //在栈区
  5. String str1 = "abc";
  6. String str2 = "abc";
  7. //在堆区 new一个对象,就会产生不同的内存地址
  8. String str3 = new String("abc");
  9. String str4 = new String("abc");
  10. //== 比较的是内存地址和内容 只要有一个不一样就为false
  11. System.out.println(str1 == str2);//T
  12. System.out.println(str1 == str3);//F
  13. System.out.println(str3 == str4);//T
  14. //a.equlas("b") 只比较内容
  15. System.out.println(str1.equals(str3));//T
  16. System.out.println(str3.equals(str4));//T
  17. }
  18. }
  19. 输出:
  20. true
  21. false
  22. false
  23. true
  24. true

2.2获取字符串长度和特定位置字符

Demo2

获取字符串长度:int string.length();

获取特定位置字符:char string.charAt(a);//a索引位置的字符

获取特定字符的位置:int string.indexOf(“c”);//c字符的下标索引位置

获取最后一个字符的位置 :int lastIndexOF(int char);

  1. package com.qfedu.test2StringClass;
  2. //返回字符串长度的方法、具体位置的字符、特定字符的位置
  3. public class Demo2 {
  4. public static void main(String[] args) {
  5. String string = "abcdefcg";
  6. //字符串的长度
  7. System.out.println(string.length());//8
  8. //获取字符串中索引为2的字符
  9. System.out.println(string.charAt(2));//c
  10. //返回指定自字符串第一次出现的字符串内的索引。
  11. System.out.println(string.indexOf("g"));//7
  12. //indexOf(int i) 括号里可以填字符 ascall码
  13. System.out.println(string.indexOf('g'));//7
  14. //返回指定自字符串最后一次出现的字符串内的索引。
  15. System.out.println(string.lastIndexOf('c'));//6
  16. //没有ac相连,输出-1
  17. System.out.println(string.indexOf("ac"));//-1
  18. //0,有ab相连的,看第一个字符
  19. System.out.println(string.indexOf("ab"));//0
  20. }
  21. }
  22. 输出:
  23. 8
  24. c
  25. 7
  26. 7
  27. 6
  28. -1
  29. 0

Demo3

以特定字符结尾:string.endsWith(“.java”); //是否以.java结尾,返回值布尔

判断字符串是否为空:string.isEmpty() //空为T 不空为F,返回值布尔

判断字符串是否包含:string.contains(“mo3”)//包含为T,返回值布尔

判断字符串是否相同:equals(String string)//返回值布尔

忽略大小写,判断两个字符串是否相等:”abc”.equalsIgnoreCase(“ABC”)

  1. package com.qfedu.test2;
  2. //以上方法
  3. public class Demo3 {
  4. public static void main(String[] args) {
  5. String string = "demo3.java";
  6. //是否以.java结束
  7. System.out.println(string.endsWith(".java"));//T
  8. System.out.println("-------------");
  9. //字符串是否为空
  10. System.out.println(string.isEmpty());//f
  11. System.out.println("".isEmpty());//T
  12. System.out.println(" ".isEmpty());//F 空格也是字符串,
  13. System.out.println("-------------");
  14. //字符串是否包含 必须相连的
  15. System.out.println(string.contains("mo3"));//T 包含
  16. System.out.println("-------------");
  17. //忽略大小写,判断两个字符相对
  18. System.out.println("abc".equals("ABC"));//F equals 不忽略大小写
  19. System.out.println("abc".equalsIgnoreCase("ABC"));// T 不区分大小写
  20. }
  21. }
  22. 输出:
  23. true
  24. -------------
  25. false
  26. true
  27. false
  28. -------------
  29. true
  30. -------------
  31. false
  32. true

2.3将字符数组转为字符串

Demo4

String的构造方法

new String(char[] char);//将字符数组转为字符串

new String(char[] char, int offset, int count); offset偏移量 count数量

static valueOf(char[] char); 静态方法 直接String.valueOf(ch2)

将字符串转为字符数组

char[] toCharArray();//把字符串变成字符数组 用for依次遍历方便看出效果

  1. package com.qfedu.test2StringClass;
  2. import java.util.Arrays;
  3. public class Demo4 {
  4. public static void main(String[] args) {
  5. //将字符数组转为字符串
  6. char[] ch1 = {'朱','志','伟'};
  7. String string = new String(ch1);
  8. System.out.println(string);
  9. //将字节数组转为字符串 会乱码
  10. byte[] by1 = {1,2,3};
  11. String string2 = new String(by1);
  12. System.out.println(string2);//会乱码
  13. //偏移量
  14. char[] ch2 = {'朱','志','伟','z','z','w'};
  15. //2:offset 偏移量 在左边开始,偏移过去2个数(从下标为2开始)
  16. //3:count 数量 取3个数据
  17. String string3 = new String(ch2, 2, 3);
  18. System.out.println(string3);//伟zz
  19. //valueOf是静态方法,可以直接类名调用,返回值是String
  20. System.out.println(String.valueOf(ch2));
  21. //把字符串变成字符数组
  22. char[] ch3 = "天气忽冷忽热".toCharArray();
  23. // System.out.println(Arrays.toString(ch3));
  24. for (int i = 0; i < ch3.length; i++) {
  25. System.out.print(ch3[i] + "\t");
  26. }
  27. }
  28. }
  29. 输出:
  30. 朱志伟
  31. ???(乱码)
  32. zz
  33. 朱志伟zzw

Demo5zhongdian

以下相对重要,,开发中要用!!!!!!

字符串替换:

String replace(char oldchar,char newchar)//把字符串中某个地方替换

切割:

String[] split(String regex); 以某字符串开始切割,切割成字符串数组

截取:

String subString(int beginIndex)

String subString(int beginIndex ,int endIndex)

大小写转化:

String toUpperCase(); 小转大

String toLowerCase(); 大转小

去除首位空格(只能去除首位的)

trim();

  1. package com.qfedu.test2StringClass;
  2. import java.util.Arrays;
  3. public class Demo5zhongdian {
  4. public static void main(String[] args) {
  5. //字符串替换 所有的都换
  6. System.out.println("abecdefg".replace("e", "换"));
  7. System.out.println("abcdefgcd".replace("cde", "换三个"));
  8. //切割
  9. String string1 = "哈哈1,呵呵1,嘿嘿1";
  10. //要求以逗号","开始切割 ==》{"哈哈"},{"呵呵"},{"嘿嘿"}
  11. String[] str1 = string1.split("1");
  12. System.out.println(Arrays.toString(str1));
  13. for (int i = 0; i < str1.length; i++) {
  14. System.out.print(str1[i] + "\t");
  15. }
  16. System.out.println();
  17. //截取
  18. String string2 = "abcdefg".substring(2);
  19. System.out.println(string2);//cdefg 下标为2以前的东西不要
  20. //第一个参数是开始截取的索引 、第二个参数是结束截取的索引
  21. //截取是“要头不要尾”,“左开右闭”
  22. String string3 = "ABCDEFGH".substring(2, 5);
  23. System.out.println(string3);//CDE
  24. //大小写字母转换
  25. //小转大
  26. System.out.println("abcAA".toUpperCase());
  27. //大转小
  28. System.out.println("QWEaa".toLowerCase());
  29. //去除空格(只能去除首位的)
  30. System.out.println(" QWE asd ");
  31. System.out.println(" QWE asd ".trim());
  32. }
  33. }
  34. 输出:
  35. abcdfg
  36. ab换三个fgcd
  37. [哈哈, ,呵呵, ,嘿嘿]
  38. 哈哈 ,呵呵 ,嘿嘿
  39. cdefg
  40. CDE
  41. ABCAA
  42. qweaa
  43. QWE asd
  44. QWE asd

3.泛型【重点】

3.1为什么要使用泛型

java1.5之后新的知识点,用来约束当前集合中的数据类型的

泛型:广泛的类型

在实际的开发中对数据一致性的要求是很重要的

例如:集合就是往里面存数据的

  1. package com.qfedu.test3fanxing;
  2. import java.util.ArrayList;
  3. //泛型 -- 用来约束当前集合中的数据类型的
  4. public class Demo1 {
  5. public static void main(String[] args) {
  6. //在真是的开发中对数据一致性的要求是很重要的
  7. //比如数组,往数组里面存数据的时候,数据类型都是一致的
  8. int[] arr = {15,94,-22};//存的数据都是int 一致的
  9. //开发中很少使用数组!为啥?需要初始化容量,有局限性
  10. //开发中使用集合来代替数组,集合也是用来存数据的
  11. //空的集合,里面可以存数据
  12. ArrayList list = new ArrayList();
  13. list.add("伍兹");
  14. list.add(24);
  15. list.add(true);
  16. list.add('人');
  17. System.out.println(list);
  18. //发现集合中的数据类型不一样!!不好
  19. //从集合中往外取数据
  20. Object object = (String)list.get(0);
  21. System.out.println(object);
  22. //涉及到强转,效率低还可能出错
  23. //java从1.5版本加上泛型,用来约束当前集合中的数据类型的
  24. ArrayList<String> list1 = new ArrayList<String>();
  25. //加上泛型以后,这个集合只能存String类型的数据,保持数据一致性
  26. list1.add("嗯");
  27. list1.add("昂");
  28. }
  29. }

3.2自定义泛型,在方法中如何书写

语法格式:

无意义的占位符:可以是T,可以是E,也可以是?。只是一个占位,没有实际的意义

  1. 权限修饰符 <无意义的占位符> 返回值 方法名字(){}
  2. public <?> void 方法名字(){}

无参数无返回值的方法:可以不加泛型,体现不到泛型的用处

有参数无返回值的方法:有必要使用泛型的

无参数有返回值的方法:没有参数没有意义,

有参数有返回值的方法

特点:

泛型方法只有带参数才能使用泛型,泛型才具有真正的意义

而且带有参数和返回值的方法,具有很大的局限性(只能传一个参数)!!!

  1. package com.qfedu.test3fanxing;
  2. public class Demo2 {
  3. public static void main(String[] args) {
  4. test("abc");
  5. test(123);
  6. //泛型
  7. test1(98.7);
  8. test1("泛型");
  9. test1(true);
  10. //无参无返回值
  11. test2();
  12. //有参无返回值
  13. test3(1, 3);
  14. test3("朱", 1);
  15. test3(true, 'z');
  16. // //无参有返回值
  17. // System.out.println(test4());
  18. //有参有返回值
  19. System.out.println(test5("狗蛋"));
  20. System.out.println(test5(30));
  21. }
  22. public static void test(String string) {
  23. System.out.println(string);
  24. }
  25. public static void test(int int1) {
  26. System.out.println(int1);
  27. }
  28. //泛型的写法 <T> T可以随便改变 告知编译器当前方法带有泛型
  29. //这个参数(T t) T 会根据你传入的实参的数据类型而随时变化
  30. public static <T> void test1(T t) {
  31. System.out.println(t);
  32. }
  33. //无参无返回值,没有体现泛型的用处
  34. public static <T> void test2() {
  35. for (int i = 0; i < 2; i++) {
  36. System.out.print("zzw");
  37. }
  38. System.out.println();
  39. }
  40. //有参无返回值,有必要使用泛型
  41. public static <T> void test3(T t,T a) {
  42. // System.out.println(t + a);//不确定什么类型不能直接相加
  43. System.out.println(t + ":" + a);
  44. }
  45. // //无参有返回值
  46. // // T代表返回值类型 T不能具体代表那一个类型,没有参数没有意义
  47. // public static <T> T test4() {
  48. // return (T) "返回谁呢";
  49. // }
  50. //有参有返回值,
  51. public static <T> T test5(T t) {
  52. return t;
  53. }
  54. }

3.3泛型类如何书写【重点】

语法格式:无意义占位符T用的偏多

  1. class 类名<无意义的占位符>{
  2. //泛型类的方法不用加<T>
  3. //在main方法中 new的时候加具体的<数据类型>
  4. //Person<String> person = new Person<String>();
  5. }

以后开发中很少用泛型类,架构师要写泛型类!!!!

  1. package com.qfedu.test4fanxingClass;
  2. //泛型类
  3. class Person<T>{
  4. //泛型类下面的方法
  5. //可以写正常的方法
  6. public void eat() {
  7. System.out.println("泛型类下可以写正常的方法,正常用正常调用");
  8. }
  9. //带泛型的方法
  10. //在泛型类上面已经定义好T,在方法中就不用在写<T>了
  11. //如果写了<T> 这个方法就是使用了自己的T的泛型,不是当前类的那个泛型
  12. // public <T> void printArgs(T t) {//打印参数
  13. // System.out.println(t);
  14. // }
  15. //方法的T 随着类的T的改变而改变
  16. public void printArgs(T t) {//打印参数
  17. System.out.println(t);
  18. }
  19. //写一个静态方法
  20. //静态的方法 类是没有办法约束的 (T t) 这个T和类的T没关系了
  21. //只能在创建对象的时候才能对对象进行约束
  22. //静态方法早于对象的创建的,对象约束不了静态方法
  23. public static <T> void staticTest(T t) {
  24. System.out.println();
  25. }
  26. }
  27. public class Demo1 {
  28. public static void main(String[] args) {
  29. // Person<String> person = new Person<String>();
  30. Person person = new Person();
  31. person.eat();
  32. //声明的时候确定数据类型,调用时只能用这个数据类型
  33. Person<String> person2 = new Person<String>();
  34. person2.printArgs("a");
  35. // person2.printArgs(45);//只能是String
  36. Person<Integer> person3 = new Person<Integer>();//只能是integer
  37. person3.printArgs(15);
  38. }
  39. }

注意【静态方法】:

  1. //在泛型类中
  2. //静态的方法 类是没有办法约束的 (T t) 这个T和类的T没关系了
  3. //只能在创建对象的时候才能对对象进行约束
  4. //静态方法早于对象的创建的,对象约束不了静态方法
  5. public static <T> void staticTest(T t) {
  6. System.out.println();
  7. }

3.4接口泛型如何书写

语法格式:

  1. interface 接口名字<无意义占位符>{
  2. }
  1. package com.qfedu.test5fanxingjiekou;
  2. //泛型接口
  3. interface A<T>{
  4. //成员方法
  5. public void test(T t);
  6. }
  7. //接口带有泛型,那么实现类也必须带有泛型
  8. class TestA<T> implements A<T>{
  9. @Override
  10. public void test(T t) {
  11. // TODO Auto-generated method stub
  12. System.out.println(t);
  13. }
  14. }
  15. public class Demo1 {
  16. public static void main(String[] args) {
  17. TestA<String> testA = new TestA<String>();
  18. testA.test("只能传String");
  19. // testA.test(18);//只能传String数据类型的
  20. TestA<Integer> testA1 = new TestA<Integer>();
  21. testA1.test(18);
  22. }
  23. }

3.4泛型抽象类如何书写

语法格式

  1. abstract class 类名<无意义占位符>{
  2. //抽象方法
  3. }
  4. 继承抽象类
  5. class A <T> extends 抽象类<T>{
  6. //重写方法
  7. }
  1. package com.qfedu.test6fanxingchouxiangClass;
  2. //泛型抽象类
  3. abstract class A<T>{
  4. //成员方法
  5. public abstract void test(T t);
  6. }
  7. class TestA<T> extends A<T>{
  8. @Override
  9. public void test(T t) {
  10. // TODO Auto-generated method stub
  11. System.out.println(t);
  12. }
  13. }
  14. public class Demo1 {
  15. public static void main(String[] args) {
  16. TestA<String> testA = new TestA<String>();
  17. testA.test("抽象类");
  18. }
  19. }