zoukankan      html  css  js  c++  java
  • ArrayList调用remove方法需要注意的地方

    ArrayList中有remove 方法和 removeAll方法,

    ArrayList中不仅继承了接口Collection中的remove方法,而且还扩展了remove方法。

    Collection中声明的接口为 public boolean remove(Object o)

                                      public boolean removeAll(Collection<?> c)

    ArrayList中含有的方法为public E remove(int index)

    实际编程中可能会通过一个Collection来调用remove接口,这种情况下不会报错,但是程序什么也不做。

    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.List;
    
    public class ArrayList01 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		List<String> allList = null;
    		Collection<String> allCollection = null;
    		
    		allList = new ArrayList<String>();
    		allCollection = new ArrayList<String>();
    		
    		allList.add("hello");
    		allList.add(0, "world");
    		System.out.println(allList);
    		allCollection.add("Yes");
    		allCollection.add("Good");
    		allList.addAll(allCollection);
    		allList.addAll(0, allCollection);
    		
    		System.out.println(allList);
    		
    		allList.remove(allCollection);
                    //allList.removeAll(allCollection);
    		System.out.println(allList);	
    	}
    
    }
    

      

    实际编程可以插入任意对象,但是如果想要通过remove(object o)来删除某个对象,这个对象必须是系统自定义的对象,如果不是的话,需要在类中覆写Object类的equals()及hashCode()方法。

    HashSet 特点:

    里面元素不能重复,而且采用散列的存储方式,没有顺序。

    TreeSet 特点:

    可以对输入的数据进行有序的排列。

    虽然如此,但是对于自定义的类,如果没有制定好排序规则,会出现异常,因此必须实现comparable接口才能正常使用TreeSet。实现comparable接口还需注意,如果对于类中某个属性没有进行比较的指定,则也会认为是同一个对象,比如Man{name = tom, score = 20}; Man { name = Jim,score = 20};如果只是指定score的比较,而没有指定名字的比较,则会认为两个Man对象是同一个对象。

    而对于HashSet,如果想要除去其中所有的重复元素,必须覆写Object类中的equals()方法和hashCode()方法,后者是表示一个哈希编码,可以理解为一个对象的编码。

    import java.util.HashSet;
    import java.util.Set;
    
    class Per{
    	private String name;
    	private int age;
    	public Per(String name, int age) {
    		// TODO Auto-generated constructor stub
    		this.name = name;
    		this.age = age;
    	}
    	
    	public boolean equals(Object obj){
    		if (this == obj) {          //地址相等时是同一个对象
    			return true;
    		}
    		if (!(obj instanceof Per)) { //传进来的不是本类的对象  不是同一个对象
    			return false;
    		}
    		
    		Per p = (Per)obj;          //进行向下转型
    		if (this.name.equals(p.name)&&this.age==p.age) {  //属性依次比较,如果相等,是同一个对象,不等不是同一个对象
    			return true;
    		}else {
    			return false;
    		}
    	}
    	
    	public int hashCode(){
    		return this.name.hashCode()*this.age;  //覆写hashCode 方法,指定编码公式
    	}
    	
    	public String toString(){
    		return "姓名:"+this.name+";  年龄: "+this.age;
    	}
    }
    
    public class SetDemo02 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Set<Per> allSet = new HashSet<Per>();
    		allSet.add(new Per("Tom", 30));
    		allSet.add(new Per("Jim", 31));
    		allSet.add(new Per("Tony", 32));
    		allSet.add(new Per("Tony", 32));
    		allSet.add(new Per("Tony", 32));
    		allSet.add(new Per("Jack", 29));
    		allSet.add(new Per("Mark", 33));
    		
    		System.out.println(allSet);
    		
    	}
    
    }
    

      

  • 相关阅读:
    javascript设计思维
    asp.net时间类-格式-方法应用
    javascript简写精练
    保存html代码
    jQuery.cookie应用操作
    ASP.NET 打包多CSS或JS文件以加快页面加载速度的Handler
    Asp.Net下通过切换CSS换皮肤
    让你的ASP.NET虚拟主机也支持子网站
    javascript获取和设置URL中的参数
    获取和设置URL里星号(#)的参数
  • 原文地址:https://www.cnblogs.com/aituming/p/4792354.html
Copyright © 2011-2022 走看看