• java例程练习(容器类简单练习)


    import java.util.ArrayList;
    import java.util.Collection;
    
    
    public class Test {
    	public static void main(String[] args) {
    		Collection<Object> c = new ArrayList<Object>();
    		
    		c.add("hello");
    		c.add(new Name("f1", "l1"));
    		c.add(new Integer(100));
    		
    		System.out.println(c.size());
    		System.out.println(c);
    	} 
    }
    
    class Name {
    	private String firstName;
    	private String lastName;
    	Name(String firstName, String lastName) {
    		this.setFirstName(firstName);
    		this.setLastName(lastName);
    	}
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
    	public String getFirstName() {
    		return firstName;
    	}
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
    	public String getLastName() {
    		return lastName;
    	}
    	public String toString() {
    		return firstName + " " + lastName;
    	}
    }
    
    //精彩继续
    import java.util.ArrayList;
    import java.util.Collection;
    
    
    public class Test {
    	public static void main(String[] args) {
    		Collection<Object> c = new ArrayList<Object>();
    		
    		c.add("hello");
    		c.add(new Name("f1", "l1"));
    		c.add(new Integer(100));
    		
    		System.out.println(c.size());
    		System.out.println(c);
    		System.out.println();
    		
    		c.remove("hello");
    		c.remove(new Integer(100));
    		
    		System.out.println(c.remove(new Name("f1", "l1")));
    		System.out.println(c);
    	} 
    }
    
    class Name {
    	private String firstName;
    	private String lastName;
    	
    	Name(String firstName, String lastName) {
    		this.setFirstName(firstName);
    		this.setLastName(lastName);
    	}
    	
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
    	
    	public String getFirstName() {
    		return firstName;
    	}
    	
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
    	
    	public String getLastName() {
    		return lastName;
    	}
    	
    	public String toString() {
    		return firstName + " " + lastName;
    	}
    	
    	public boolean equals(Object obj) {
    		if(obj instanceof Name) {
    			Name name = (Name) obj;
    			return (firstName.equals(name.firstName)) && 
    				   (lastName.equals(name.lastName));
    		}
    		return super.equals(obj);
    	}
    	
    	public int hashCode() {
    		return firstName.hashCode();
    	}
    }
    


    
    


  • 相关阅读:
    AD转化器分类及特点和选用
    采样频率 、采样率
    RGBA alpha 透明度混合算法
    SDRAM中数据掩码的作用
    Allegro---层叠结构设置
    PCB主线布线规范—高速线之DDR2
    Allegro 反射仿真--拓扑结构的提取提取及波形分析
    Allegro 反射仿真--仿真设置
    Allegro 反射仿真--IBIS模型转化
    SigXplorer设置延时及Local_Global
  • 原文地址:https://www.cnblogs.com/wjchang/p/3671621.html
Copyright © 2020-2023  润新知