[七]java作业

抛出java子类异常

class myException1 extends Exception{
	private static final long serialVersionUID = 1240520315986594291L;
	public myException1(){
		System.out.printf("error\n");
	}
	public myException1(String str){
		System.out.printf(str);
	}
}

class myCount{
	 public double div(double i,double j)throws myException1{
		 if(j==0){
			 throw new myException1();
		 }
		 return i/j;
	 }
 }

public class throwstest {

	public static void main(String[] args)throws Exception {
		// TODO 自动生成的方法存根
		myCount test=new myCount();
		try{
			test.div(1, 0);
		}catch(myException1 e){
			System.out.printf("myException1");
		}catch(Exception e){
			System.out.printf(e.getMessage());
		}
	}
}
/*
output:
error
myException1
*/

collection集合判断子串包含

import java.util.*;

public class collectiontest {

	public static void main(String[] args) {
		Set<String> str1=new HashSet<String>();
		Set<String> str2=new HashSet<String>();
		
		str1.add("1");
		str1.add("2");
		str1.add("3");
		str1.add("4");
		str1.add("5");
		str1.add("6");
		str1.add("7");
		str1.add("8");
		
		str2.add("4");
		str2.add("5");
		str2.add("6");
		
		if(str1.containsAll(str2)==true){
			System.out.printf("same\n");
		}
		else{
			System.out.printf("not same\n");
		}
		
		str2.add("9");
		if(str1.containsAll(str2)==true){
			System.out.printf("same\n");
		}
		else{
			System.out.printf("not same\n");
		}
	}
}
/*
output:
same
not same
*/

hashset 实现类覆盖equals hashcode

import java.util.*;

class human{
	private String name;
	private int idCard;
	public human(String name,int idCard){
		this.name=name;
		this.idCard=idCard;
	}
	public int hashCode(){
		return idCard;
	}
	public boolean equals(Object obj){
		if(obj==null){
			return false;
		}
		if(obj instanceof human ==false){
			return false;
		}
		human u=(human)obj ;
		if(u.idCard()!=this.idCard){
			return false;
		}
		System.out.println("had same Object");
		return true;
	}
	public String name(){
		return name;
	}
	public int idCard(){
		return idCard;
	}
	public String toString(){
		return "it is "+name+" ID is "+idCard;
	}
}

public class test {

	public static void main(String[] args) {
		human hs3=new human("张三",3);
		human hs1=new human("张三",1);
		human hs2=new human("张三",1);
		
		Set<human>set=new HashSet<human>();
		set.add(hs1);
		set.add(hs2);
		set.add(hs3);
		
		for(human hashsetuser:set){
			System.out.println(hashsetuser);
		}
	}
}
/*
output:
had same Object
it is 张三 ID is 1
it is 张三 ID is 3
*/

treeset 实现类

import java.util.*;

class human implements Comparable<human>{
	private String name;
	private int idCard;
	public human(String name,int idCard){
		this.name=name;
		this.idCard=idCard;
	}
	public int hashCode(){
		return idCard;
	}
	public boolean equals(Object obj){
		if(obj==null){
			return false;
		}
		if(obj instanceof human ==false){
			return false;
		}
		human u=(human)obj ;
		if(u.idCard()!=this.idCard){
			return false;
		}
		System.out.println("had same Object");
		return true;
	}
	public String name(){
		return name;
	}
	public int idCard(){
		return idCard;
	}
	public String toString(){
		return "it is "+name+" ID is "+idCard;
	}
	@Override
	public int compareTo(human o) {
		if(this.idCard>o.idCard){
			return -1;
		}else if(this.idCard<o.idCard){
			return 1;
		}
		return 0;
	}
	
}

public class test {

	public static void main(String[] args) {
		human hs1=new human("张三",3);
		human hs2=new human("张三",1);
		human hs3=new human("张三",2);
		human hs4=new human("张三",4);
		human hs5=new human("张三",1);
		
		Set<human>set=new TreeSet<human>();
		
		set.add(hs1);
		set.add(hs2);
		set.add(hs3);
		set.add(hs4);
		set.add(hs5);
		
		for(human hashsetuser:set){
			System.out.println(hashsetuser);
		}
	}
}
/*
output:
it is 张三 ID is 4
it is 张三 ID is 3
it is 张三 ID is 2
it is 张三 ID is 1
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值