程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> List.contains(Object object)方法

List.contains(Object object)方法

編輯:C++入門知識

List.contains(Object object)方法


使用List.contains(Object object)方法判斷ArrayList是否包含一個元素對象(針對於對象的屬性值相同,但對象地址不同的情況),如果沒有重寫List的元素對象Object中的equals方法,默認如下:

 

	@Override
	public boolean equals(Object o) {
		// TODO Auto-generated method stub
		return super.equals(o);
	}

將導致contains方法始終返回false。

 

查看ArrayList的contains方法的源碼如下:

    /**
     * Searches this {@code ArrayList} for the specified object.
     *
     * @param object
     *            the object to search for.
     * @return {@code true} if {@code object} is an element of this
     *         {@code ArrayList}, {@code false} otherwise
     */
    @Override public boolean contains(Object object) {
        Object[] a = array;
        int s = size;
        if (object != null) {
            for (int i = 0; i < s; i++) {
                if (object.equals(a[i])) {
                    return true;
                }
            }
        } else {
            for (int i = 0; i < s; i++) {
                if (a[i] == null) {
                    return true;
                }
            }
        }
        return false;
    }

可以看出,contains方法依據Object的equals方法來判斷是否包含某一元素,繼續查看Object類中的equals方法,源碼如下:

 

    public boolean equals(Object o) {
        return this == o;
    }

所以,使用“==”比較對象的地址,如果是同一對象即地址相同的情況下,才會返回true,而對於對象屬性值相同但地址不同的不同對象,始終返回false!

 

如果需要依據對象屬性值是否相同來判斷ArrayList是否包含某一對象,則需要重寫Object的equals方法,並在equals方法中一一比較對象的每個屬性值,如:

 

package com.feng.lejuan.entity;

public class QuestionInfo {

	private String questionId;
	
	private String answerId;
	
	private String subQuestionId;
	
	private String result;

	public QuestionInfo() {
		super();
		
	}

	public QuestionInfo(String questionId, String answerId,
			String subQuestionId, String result) {
		super();
		this.questionId = questionId;
		this.answerId = answerId;
		this.subQuestionId = subQuestionId;
		this.result = result;
	}

	public String getQuestionId() {
		return questionId;
	}

	public void setQuestionId(String questionId) {
		this.questionId = questionId;
	}

	public String getAnswerId() {
		return answerId;
	}

	public void setAnswerId(String answerId) {
		this.answerId = answerId;
	}

	public String getSubQuestionId() {
		return subQuestionId;
	}

	public void setSubQuestionId(String subQuestionId) {
		this.subQuestionId = subQuestionId;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	@Override
	public boolean equals(Object o) {
		if (o instanceof QuestionInfo) {
			QuestionInfo question = (QuestionInfo) o;
			return this.questionId.equals(question.questionId)
					&& this.subQuestionId.equals(question.subQuestionId)
					&& this.answerId.equals(question.answerId)
					&& this.result.equals(question.result);
		}
		return super.equals(o);
	}
	
	@Override
	public String toString() {
		return "QuestionInfo [questionId=" + questionId + ", answerId="
				+ answerId + ", subQuestionId=" + subQuestionId + ", result="
				+ result + "]";
	}
	
}


 


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved