1.
@Test public void SetTest() { Set<String> set = new HashSet<>(); boolean b = set.add("two"); System.out.println("b = " + b); // true 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] System.out.println("-----------------"); b = set.add("one"); System.out.println("b = " + b); // false System.out.println(set); // [one, two, three] }
2.
//练习:随机生成10个1~20之间的随机数放入Set集合并打印 @Test 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--; } }*/ while (set.size() < 10) { set.add(random.nextInt(20) + 1); } System.out.println(set); }
3.
@Test 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); String value = map.get(3); System.out.println("3 = " + value); boolean b = map.containsKey(5); System.out.println(b); // false b = map.containsKey(2); System.out.println(b); // true b = map.containsValue("斯坦尼斯拉夫斯基"); System.out.println(b); // false b = 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); }
4.
@Test 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) { System.out.println(it + "=" + map.get(it)); } System.out.println("-----------------------------------"); //使用entrySet遍历Map集合中的元素,方式三 Set<Map.Entry<Integer, String>> entries = map.entrySet(); for (Map.Entry<Integer, String> e : entries) { System.out.println(e); } }
5.
@Test public void ExceptionTest01() { //ArithmeticException int a = 10; int b = 0; if (b != 0) { System.out.println(a / b); } //ArrayIndexOutOfBoundsException 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 str2 = "123aa"; //匹配正则表达式 \d+表示数字 if (str2.matches("\\d+")) { System.out.println(Integer.parseInt(str2)); } System.out.println("程序执行完毕!!!"); }
6.
public class Person { String name; int age; public Person(String name, int age) throws AgeException { setAge(age); setName(name); } 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("年龄不合理!"); } } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
public class AgeException extends Exception{ public AgeException() { } public AgeException(String msg) { super(msg); } }
public class PersonTest { public static void main(String[] args){ Person person = null; try { person = new Person("张辉",-10); } catch (AgeException e) { e.printStackTrace(); } System.out.println(person); } }
7.
public class ExceptionTest001 { public static void main(String[] args) { try { System.out.println(10 / 0); System.out.println("陪伴才是最长情的告白!"); } catch (ArithmeticException e) { System.out.println("出异常了"); // System.exit(0); } finally { System.out.println("我是finally,你看看我有没有打印内容!"); } } }
8.
public class ExceptionTest002 { 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; } }
9.
public class ExceptionTest003 { 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; } } }