package day07;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
public class SetTest {
@Test
public void setTest(){
Set<String> set=new HashSet<>();
boolean b=set.add("two");
System.out.println("b="+b);
System.out.println(set);
b=set.add("one");
System.out.println("b="+b);
System.out.println(set);
b=set.add("three");
System.out.println("b="+b);
System.out.println(set);
b=set.add("one");
System.out.println("b="+b);
System.out.println(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);
}
@Test
public void hashMapTest(){
Map<Integer,String> map=new HashMap<>();
map.put(1,"xiaonan");
map.put(2,"mingyang");
map.put(3,"xingjie");
map.put(4,"miaomiao");
System.out.println(map);
String value=map.get(3);
System.out.println("3="+value);
boolean b=map.containsKey(5);
System.out.println(b);
b=map.containsKey(2);
System.out.println(b);
b=map.containsKey("fandouhuayuan");
System.out.println(b);
b=map.containsKey("tutu");
System.out.println(b);
//删除
String v=map.remove(3);
System.out.println("删除的值为:"+v);
v=map.remove(11);
System.out.println("删除的值为:"+v);
}
@Test
public void iteratorTest(){
Map<Integer,String> map=new HashMap<>();
map.put(1,"xiaonan");
map.put(2,"mingyang");
map.put(3,"xingjie");
map.put(4,"miaomiao");
//1.Map遍历
System.out.println(map);
System.out.println("--------------");
//2.keySet遍历
Set<Integer> set=map.keySet();
for (Integer it:set){
System.out.println(it+"="+map.get(it));
}
System.out.println("-------------");
//3.entrySet遍历
Set<Map.Entry<Integer,String>> entries=map.entrySet();
for (Map.Entry<Integer,String> e:entries){
System.out.println(e);
}
}
@Test
public void exceptionTest(){
//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";
if(str2.matches("\\d+")){
System.out.println(Integer.parseInt(str2));
}
System.out.println("运行结束了");
}
@Test
public void exceptionTest01(){
try {
FileInputStream fileInputStream=new FileInputStream("c:/a.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
package day07;
public class ExceptionTest {
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,你看看我有没有打印内容");
}
}
}
java实训第七天
最新推荐文章于 2024-08-05 16:41:06 发布