import java.util.Arrays; public class ListOfMy{ private Object[] obj; private int num=0; public ListOfMy(){ obj=new Object[0]; } public int size(){ return num; } public boolean isEmpty() { if(obj==null || obj.length<1){ return true; } return false; } public boolean contains(Object element) { for(int i=0; i<obj.length; i++){ if(obj[i].equals(element)){ return true; } } return false; } public boolean add(Object element) { if(element!=null){ num++; obj=Arrays.copyOf(obj, num); obj[num-1]=element; return true; } return false; } public boolean remove(Object element){ boolean f=false; for(int i=0,j=0; i<obj.length; i++){ if(!obj[i].equals(element)){ obj[j++]=obj[i]; f=true; } } if(f){ obj=Arrays.copyOf(obj, --num); } return f; } public void clear() { obj=new Object[0]; num=0; } public Object get(int index)throws Exception{ if(indexnum){ throw new Exception("index無(wú)效"); } return obj[index]; } public int set(int index, Object element)throws Exception{ if(indexnum){ throw new Exception("index無(wú)效"); } obj[index]=element; return index; } public void insert(int index, Object element)throws Exception{ if(indexnum){ throw new Exception("index無(wú)效"); } num++; obj=Arrays.copyOf(obj, num); for(int i=0; iindex; i--){ obj[i]=obj[i-1]; } obj[index]=element; } public Object remove(int index)throws Exception{ if(indexnum){ throw new Exception("index無(wú)效"); } Object ob=null; for(int i=0, j=0;i<num;i++){ if(i!=index){ obj[j]=obj[i]; j++; }else{ ob=obj[i]; } } if(null!=ob){ obj=Arrays.copyOf(obj, --num); } return ob; } public int indexOf(Object element) { for(int i=0; i<obj.length; i++){ if(obj[i].equals(element)){ return i; } } return -1; } public String toString(){ StringBuilder sb=new StringBuilder("["); for(int i=0; i0)sb.append(","); sb.append(obj[i]); } sb.append("]"); return sb.toString(); } } 基本的,請(qǐng)參數(shù),有不對(duì)的地方,請(qǐng)自己調(diào)試、修改。
可以通過(guò)繼承ArrayList,然后在派生類重寫(xiě)toString方法,例如:
import static java.lang.System.out;
public class Program {
public static void main(String[] args) throws Exception {
MyArrayList<String> myArrayList=new MyArrayList<String>(){{
add("test1");
add("test2");
}};
for(String item:myArrayList){
out.println(item);
}
out.println(myArrayList);// print This override toString method.
}
}
class MyArrayList<E> extends ArrayList<E>{
@Override
public String toString() {
return "This override toString method.";
//return super.toString();
}
}
使用set集合啊,set集合是不能放重復(fù)的數(shù)據(jù)的。
Set hashSet = new HashSet(list);然后在轉(zhuǎn)過(guò)來(lái)就行了,set去除重復(fù)是調(diào)用了equals方法,所以你在Sh里面要重寫(xiě)equals方法,自定義比較的規(guī)則,例如:public boolean equals(Object obj) { if(obj==null) return false; if(obj==this) return true; Sh sh = (Sh)obj; if(sh.getHm()==this.getHm()){ return true; } return false; }。
簡(jiǎn)單介紹Object中的equals()方法和HashCode()方法:java中的String,Integer這些類已經(jīng)實(shí)現(xiàn)了equals和HashCode方法的重寫(xiě),但是Object類中并沒(méi)有重寫(xiě)
equals():
equals()方法,在這些類中equals()方法的實(shí)現(xiàn)是:
public boolean equals(Object obj){
return(this == obj);
}
HashCode()方法
比較的是兩個(gè)對(duì)象的內(nèi)存地址,在Object中的定義是:public native int hashCode();
說(shuō)明他只是一個(gè)本地方法,要了解HashCode就要了解java中集合,一般來(lái)說(shuō)分為兩類,一類是List,一類是Set。前者元素有序可以重復(fù),后者無(wú)需不能重復(fù)。在Set中有HashMap,HashSet這些方法就是保證元素的不重復(fù)性。
兩者關(guān)系:
要判斷兩個(gè)對(duì)象是否相等,就要重寫(xiě)equals()方法,具體的實(shí)現(xiàn)網(wǎng)上很多。重寫(xiě)equals()方法一般都要重寫(xiě)HashCode()方法,這是為了提高存儲(chǔ)效率,上面說(shuō)的HashCode可以保證相同的元素存儲(chǔ)于相同的地址,理論上可以沒(méi)有,但是如果沒(méi)有,使用效率會(huì)大大降低。比如說(shuō)String實(shí)現(xiàn)HashCode之后,相同的對(duì)象對(duì)應(yīng)的地址相同,然后在進(jìn)行equals比較,里面對(duì)象都存儲(chǔ)在堆中的String Pool里面。
希望對(duì)你有所幫助
例如
class A
{
public void overWrite()
{
System.out.print("A")
}
}
public class B extends A
{
public void overWrite()
{
System.out.print("B")
}
public static void main(String[] args)
{
A a=new B();
a.overWrite();
}
}
輸出B,這就是重寫(xiě),主要發(fā)生在之類重寫(xiě)父類的方法和屬性.
可能你是剛學(xué)習(xí)這門語(yǔ)言吧?其實(shí)很簡(jiǎn)單就可以知道答案了啊.設(shè)個(gè)斷點(diǎn),然后一步步F5(eclipse環(huán)境下)不就知道答案了嗎?
當(dāng)然我得承認(rèn)我一開(kāi)始想當(dāng)然地認(rèn)為在所有的集合類的總父接口里統(tǒng)一override了toString()方法.斷點(diǎn)一看才知道不是這么回事,
事實(shí)上,是在這個(gè)類里實(shí)現(xiàn)的:
public abstract class AbstractCollection<E> implements Collection<E>
在其中有一個(gè)toString()的重寫(xiě)
public String toString() {
Iterator<E> i = iterator();
if (! i.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = i.next();
sb.append(e == this ? "(this Collection)" : e);
if (! i.hasNext())
return sb.append(']').toString();
sb.append(", ");
}
}
建議你自己隨便新建一個(gè)list或者map什么的,用斷點(diǎn)的方法去看看程序到底怎么走的.
聲明:本網(wǎng)站尊重并保護(hù)知識(shí)產(chǎn)權(quán),根據(jù)《信息網(wǎng)絡(luò)傳播權(quán)保護(hù)條例》,如果我們轉(zhuǎn)載的作品侵犯了您的權(quán)利,請(qǐng)?jiān)谝粋€(gè)月內(nèi)通知我們,我們會(huì)及時(shí)刪除。
蜀ICP備2020033479號(hào)-4 Copyright ? 2016 學(xué)習(xí)鳥(niǎo). 頁(yè)面生成時(shí)間:2.570秒