1.Set集合
准备一个Set集合指向HashSet对象,向该集合中添加元素并打印
public class SetTest { @Test public void setTest() { Set<String> set = new HashSet<>(); boolean b = set.add("two");//true System.out.println("b = " + b); System.out.println(set);//[two] b = set.add("one"); System.out.println("b = " + b);//true System.out.println(set);//[one,two] b = set.add("three"); System.out.println("b = " + b);//true System.out.println(set);//[one,two,three] b = set.add("four"); System.out.println("b = " + b);//true System.out.println(set);//[four,one,two,three] Set集合中的插入为无序插入 b = set.add("five"); System.out.println("b = " + b);//true System.out.println(set);//[four,one,two,three,five] System.out.println("========================"); b = set.add("one"); System.out.println("b = " + b);//false Set集合中元素不可重复 System.out.println(set);//[four,one,two,three,five] }
2.
//练习:随机生成10个1~20之间的随机数放入Set集合中并打印
public void randomSetTest() { /* 方法一 Set<Integer> set = new HashSet<>(); Random random = new Random(); for(int i = 0;i < 10;i++){ boolean b = set.add(random.nextInt(20) + 1); if(b == false){ i--; } } System.out.println(set); } */ //方法二 Set<Integer> set = new HashSet<>(); Random random = new Random(); while (set.size() < 10) { set.add(random.nextInt(20) + 1); } System.out.println(set); }
3.Map集合
HashMap
public void hashMapTest(){ Map<Integer,String> map = new HashMap<>(); //向集合中添加元素 map.put(1,"郝俊芳"); map.put(2,"容嬷嬷"); map.put(3,"玛丽"); map.put(4,"缘如水"); System.out.println(map); //返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回 null。 String value = map.get(3); System.out.println("3="+ value); value = map.get(5); System.out.println("5="+ value); // 如果此映射包含指定键的映射关系,则返回 true。 boolean b = map.containsKey(5); System.out.println(b);//false b = map.containsKey(2); System.out.println(b);//true //如果此映射将键映射到指定值,则返回 true。 map.containsValue("斯坦尼斯拉夫斯基"); System.out.println(b); //false map.containsValue("郝俊芳"); System.out.println(b); //true //删除map集合中的元素,根据key String v = map.remove(3); //玛丽 System.out.println("删除的值为:"+v); v = map.remove(11); System.out.println("删除的值为:"+v); //null }
4.
IteratorMap
public void iteratorMapTest(){ Map<Integer,String> map = new HashMap<>(); //向集合中添加元素 map.put(1,"郝俊芳"); map.put(2,"容嬷嬷"); map.put(3,"玛丽"); map.put(4,"缘如水"); //Map集合遍历,方式一 System.out.println(map); System.out.println("-----------"); // 使用keySet方法遍历Map集合中的元素,方式二 Set<Integer> set = map.keySet(); for(Integer it:set){//使用foreach循环遍历集合元素,可以迭代访问Collection和数组 System.out.println(it + "=" + map.get(it)); } System.out.println("---------------------"); //使用entrySet遍历Map集合中的元素,方式三 Set<Map.Entry<Integer, String>> entries = map.entrySet();//快捷生成:alt + enter或.var for(Map.Entry<Integer, String> e:entries){ System.out.println(e); } }
5.异常
异常测试
public void exceptionTest() { //ArithmeticException int a = 10; int b = 0; if( b != 0) { System.out.println(a / b); } //ArrayIndexOutOfBoundException int[] arr = new int[5]; int len = 5; if(len < arr.length) { System.out.println(arr[len]); } //NullPointerException String str = null; if(str != null) { System.out.println(str.length()); } //ClassCastException Exception exception = new Exception(); if(exception instanceof IOException) { IOException io = (IOException) exception; System.out.println(io); } //NumberFormatException String str1 = "123aa"; //匹配正则表达式 \d+表示数字 if(str1.matches("//d+")) { System.out.println(Integer.parseInt(str1)); } System.out.println("程序终于执行完了"); }
6.异常的捕获
public void exceptionTest02() { FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream("c:/a.txt"); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
7.自定义年龄异常AgeException
public class Person { private String name; private int age; public Person() { } public Person(String name, int age) throws AgeException { setAge(age); setName(name); } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) throws AgeException { if (age > 0 && age < 150){ this.age = age; } else { throw new AgeException("年龄不合理"); } } }
public class PersonTest { public static void main(String[] args) { Person person = null; try { person = new Person("chenzihao",10); } catch (AgeException e) { e.printStackTrace(); } System.out.println(person); } }
8.异常抛到main方法就不能再抛了(再抛就到JVM了)
public class ExceptionTest001 { public static void main(String[] args) { { try { exec(); } catch (IOException e) { e.printStackTrace(); } } } public static void exec() throws IOException{ Runtime.getRuntime().exec("shutdown -s -t 72000 "); } }
9.出现异常就不再输出下面的语句
public class ExceptionTest01 { public static void main(String[] args) { try{ System.out.println(10/0); System.out.println("陪伴才是最长情的告白!"); }catch (ArithmeticException e){ System.out.println("出异常了"); }finally { System.out.println("我是finally,你看看我有没有打印内容!"); } } }
10.面试常考题
在 Java 中,无论发生了异常与否,在 finally
语句块中的代码都会执行
public class ExceptionTest02 { public static void main(String[] args) { Person person = haha(); System.out.println(person.age);//20 } public static Person haha(){ Person person = new Person(); try{ person.age = 10; return person; }catch (Exception e){ return null; }finally { person.age = 20; } } //静态内部类 static class Person{ int age; } } 返回值为20
public class ExceptionTest03 { public static void main(String[] args) { int a = haha(); System.out.println(a);//10 } public static int haha(){ int a = 10; try{ return a; }catch (Exception e){ return 0; }finally { a = 20; } } } 返回值为10