一.HashSet的特性(不会重复放入数据)
以及所写相关程序:集合中放入10个不重复的1-20的随机数并打印,用HashSet实现
import java.util.*; public class SetTest { public static void main(String[] args) { Set<String> set = new HashSet<>(); boolean b = set.add("two"); System.out.println(b); System.out.println(set); set.add("one"); System.out.println(set); set.add("three"); System.out.println(set); set.add("five"); System.out.println(set); set.add("four"); System.out.println(set); set.add("three"); System.out.println(set); set.add("three"); System.out.println(set); //集合中放入10个1-20的随机数并打印 Set<Integer> set1 = new HashSet<>(); Random random = new Random(); System.out.println(set1.size()); while(set1.size()<10){ set1.add(random.nextInt(20)+1); System.out.println(set1); } } }
二.HashMap类中的常用方法,HashMap的三种遍历方法
import java.util.*; public class MapTest { public static void main(String[] args) { Map<Integer,String> map = new HashMap<>(); map.put(1,"hh"); map.put(2,"xx"); map.put(3,"ss"); map.put(4,"zz"); //map集合遍历方法一: System.out.println(map); String value = map.get(3); System.out.println(value); boolean b = map.containsKey(5); System.out.println(b); b= map.containsValue("hh"); System.out.println(b); String v = map.remove(2); System.out.println(v); System.out.println(map); //使用keyset方法遍历map集合中的元素 System.out.println(map.keySet()); Set<Integer> set = map.keySet(); for(Integer it : set){ System.out.println(it + "=" + map.get(it)); } //使用entryset方法遍历map集合中的元素 Set<Map.Entry<Integer,String>> entries= map.entrySet(); for(Map.Entry<Integer,String> o:entries){ System.out.println(o); } } } 三.自定义异常(判断年龄是否为负数)
public class Person { private String name; private int 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<150&&age>0){ this.age = age; }else{ throw new AgeException("年龄不正确"); } } public Person(){} public Person(String name, int age) throws AgeException{ setName(name); setAge(age); } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
public class PersonTest { public static void main(String[] args) { Person person = null; try { person = new Person("zzd",-10); } catch (AgeException e) { e.printStackTrace(); } System.out.println(person); } }
public class AgeException extends Exception{ public AgeException() {} public AgeException(String msg){ super(msg); } }
四.看看 trycatch中哪些话可以输出哪些不行
(finally中的语句都输出(存在特殊情况),try中有异常的语句直接中断不输出后面)
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"); } } } 五.
比较ExceptionTest02和ExceptionTest03的输出结果说明原因
ExceptionTest02:10
ExceptionTest03: 20
因为基本数据类型是将数字10直接返回,之后再从栈内存中修改a 的值
而引用数据类型是将栈内存中指向堆内存的地址返回,堆内存已将数据修改
如图:
public class ExceptionTest02 { public static void main(String[] args) { int b = haha(); System.out.println(b); } public static int haha(){ int a; try{ a = 10; return a; }catch(Exception e){ return 0; }finally { a = 20; } } }
public class ExceptionTest03 { public static void main(String[] args) { Person person = haha(); System.out.println(person.age); } 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; } }