zoukankan      html  css  js  c++  java
  • 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();
    	}
    }
    


    
    


  • 相关阅读:
    文件操作
    通过类型断言获取error类型,获得更详细的信息

    数组
    使用unsafe改善性能
    使用unsafe.Pointer将结构体转为[]byte
    (GoRails)链接link_to到当前页current Page 并使用参数 (类ActionController::Parameters)
    用ActionController::Renderer的render方法渲染模版
    innerHTML用法及错误:无法设置未定义或null引用的属性“innerHTML”解决
    ActionCable的部署(参考Gorails)
  • 原文地址:https://www.cnblogs.com/wjchang/p/3671621.html
Copyright © 2011-2022 走看看