java入門 -- List集合接口的實(shí)現(xiàn)類ArrayLIst以及集合去重

import java.util.ArrayList;

import java.util.Iterator;

/*

* Collection

* -----| List 實(shí)現(xiàn)List接口的集合類,其對(duì)象中的元素是有序且可重復(fù)

* ---------| ArrayList List接口實(shí)現(xiàn)類 特點(diǎn)是維護(hù)了一個(gè)ArrayList數(shù)組實(shí)現(xiàn)的,特點(diǎn):查詢快,增刪慢;因?yàn)閿?shù)組的地址是連續(xù)的,

* 所以查詢速度快,而增刪需要新建數(shù)組拷貝原值,特別是數(shù)據(jù)量極大的時(shí)候,增刪就會(huì)非常之慢。如果增刪比較少,查詢比較多久使用

* ArrayLIst類

* ---------| LinkList? List接口實(shí)現(xiàn)類

* ---------| Vector List接口實(shí)現(xiàn)類

* -----| Set? 實(shí)現(xiàn)Set接口的集合類,其對(duì)象中的元素是無序且不可重復(fù)

*

* List接口的實(shí)現(xiàn)類:

* ArrayList 特有方法

* ensureCapacity() 指定容量,一般用帶參的構(gòu)造方法去指定

* trimToSize() 裁剪,將容量裁剪至最少,一般也不適用.

*

* 構(gòu)造方法: ArrayList() 無參構(gòu)造方法,構(gòu)造一個(gè)容量為10的空集合對(duì)象

* ArrayList的內(nèi)部維護(hù)了一個(gè)Object的對(duì)象數(shù)組,使用無參的構(gòu)造函數(shù)創(chuàng)建對(duì)象時(shí),默認(rèn)的容量是10,

* 當(dāng)容量不夠用的時(shí)候,長(zhǎng)度自動(dòng)增長(zhǎng)0.5倍.

*

*/

//定義人類

class Person{

String name; //姓名

int id; //ID號(hào)

public Person(){}

public Person(int id, String name){

this.id = id;

this.name = name;

}

@Override

public int hashCode() {

// TODO Auto-generated method stub

return this.id;

}

@Override

public boolean equals(Object obj) {

// TODO Auto-generated method stub

Person p = (Person)obj;

return this.id == p.id;

}

@Override

public String toString() {

// TODO Auto-generated method stub

return "{" + this.id +"," + this.name +"}";

}

}

public class Demo3 {

public static void main(String[] args){

//使用迭代器去重

ArrayList al = new ArrayList();

al.add(new Person(100,"張山"));

al.add(new Person(100,"李四"));

al.add(new Person(101, "王五"));

al.add(new Person(101, "王五"));

al.add(new Person(102, "李六"));

al.add(new Person(102, "李六"));

al.add(new Person(102, "李六"));

al = distinct(al);

System.out.println(al);

}

public static ArrayList distinct(ArrayList al){

ArrayList temp = new ArrayList();

Iterator iter = al.iterator();

Person tp = new Person();

while(iter.hasNext()){

tp = (Person)iter.next();

if(temp.contains(tp)){

continue;

}

else{

temp.add(tp);

}

}

return temp;

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,628評(píng)論 18 399
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,525評(píng)論 19 139
  • 多態(tài) 任何域的訪問操作都將有編譯器解析,如果某個(gè)方法是靜態(tài)的,它的行為就不具有多態(tài)性 java默認(rèn)對(duì)象的銷毀順序與...
    yueyue_projects閱讀 1,088評(píng)論 0 1
  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,891評(píng)論 0 33
  • 早晨七點(diǎn)半起床,是一骨碌那種,匆匆的洗臉?biāo)⒀阑瘖y換衣服,充電器耳機(jī)胡亂的塞進(jìn)帆布包里,水也來不及喝一口,七點(diǎn)五十了...
    綠耳朵z閱讀 431評(píng)論 0 0

友情鏈接更多精彩內(nèi)容