day(13)The Basic Use of ArrayList<>

Collection

Collection is an interface of collection class,which defines common methods in collection(集合共有的方法)


-|List(列表) (An Array)

Duplicate objects are allowed &It is sequential(有序的)

ArrayList

linear list structure(線性表結(jié)構(gòu))
Methods connected with 'get' are used frequently.
Pay attention to the ‘remove' method uesd in for-loop.
iterator
remove() & removeIf()
contains();
set();

class Person implements Comparable<Person> {
    protected float score;
    protected String name;
    protected int age;

    public Person(String name,float score,int age){
        this.name = name;
        this.score = score;
        this.age = age;

    }
    @Override
    public String toString() {
        return "name:"+name+ " "+"score:" + score + " "+"age:"+age;
    }

    //  Ascending order(升序)
    @Override
    public int compareTo(Person person) {
        // age can be score as well
        if(age > person.age){
            return 1;
        }else if(age == person.age){
            return  0;
        }
        return -1;
    }

    //  Descending order(降序)
    /*public int compareTo(Person person) {
        if(age > person.age){
            return -1;
        }else if(age == person.age){
            return  0;
        }
        return 1;
    }*/
}

public class MyClass {
    public static void main(String[] args) {
        //  Create an array
        //  polymorphism
        //  The Collection Object can only save one same kind of data
        List<Person> people = new ArrayList<>();
        ArrayList<Person> personList = new ArrayList<>();

        //  Add an object at the end of array
        //Person xw = new Person("小王",90,15);
        personList.add(new Person("Jack",90,15));
        personList.add(new Person("John",90,16));
        personList.add(new Person("Merry",90,17));
        //  Add data in a certain position
        personList.add(0,new Person("小王",90,14));

        //  Get one object
        Person person = personList.get(0);
        System.out.println(personList);

        // Traverse this array
        //1. for cyclic sentences
        for (int i = 0; i < personList.size(); i++){
            Person p = personList.get(i);
            // ↓ Delete the corresponding object,but this exists problems
            // ↓ And this normally won't be the method we want
            //personList.remove(i);
            //  Delete one certain object
            //personList.remove(p);
            //System.out.println(p);
        }
        System.out.println("______for-each________");

        // 2.The enhanced for-each loop(增強(qiáng)for循環(huán))
        for(Person p : personList){
            //  Fail
            //personList.remove(p);
            System.out.println(p);
        }

        System.out.println("_______iterator_______");
        //  3.Ues Iterator (使用Iterator遍歷器)
        // Main difference: The object will be deleted when traversed

        // Get the corresponding iterator object
        Iterator<Person> iterator = personList.iterator();
        //  Ues hasNext() to judge whether exists another one object
        //  Use next() to get the next object
       /* while(iterator.hasNext()){
            Person p = iterator.next();
            //personList.remove(p);
           iterator.remove();
        }*/
        System.out.println("*********");
        System.out.println(personList);


        System.out.println("*****removeIf******");
        //  The Delete of the Data
        //  1.Delete the data under certain condition
        //  Create a subclass object of Predicate Interface
        //MyPredicate predicate = new MyPredicate();
        //personList.removeIf(predicate);
        //System.out.println("****removeIf***");
        //System.out.println(personList);

        //  2.Use a anonymous inner class(匿名內(nèi)部類)
        /*personList.removeIf(new Predicate<Person>() {
            @Override
            public boolean test(Person person) {
                if(person.age > 15){
                    // If it is true ,this object will be deleted
                    return true;
                }
                return false;
            }
        });*/


        // Sort(排序)
        // 1.The defined class by oneself should implements the Comparable Interface ↑(Person class) and override compareTo method
        //Collections.sort(personList);
        personList.sort(new Comparator<Person>() {
            @Override
            public int compare(Person person, Person t1) {
                //  Make your own rules of comparison
                return person.name.compareTo(t1.name);
            }
        });

        System.out.println(personList);

        //  Get one object
        Person person1 = personList.get(0);

        //  This is a new object.corresponding to a new person
        person = new Person("John",90,16);

        // Judge whether this object contains certain element
        if (personList.contains(person)){
            System.out.println("Here exist this person");
        }else{
            System.out.println("Here doesn't exist this person");
        }

        //  Get index of certain object
        System.out.println(personList.indexOf(person1));

        // To change the index of certain object
        personList.set(0,person1);

        // Clear all data
        personList.clear();
        System.out.println(personList);
    }

    // Create a class to implement the methods in the Predicate Interface
    //  For the 'removeIf' method
    static class MyPredicate implements Predicate<Person> {
        @Override
        public boolean test(Person person){
            if(person.age > 15 ){
                return true;
            }
            return false;
        }
    }
}

LinkList

chain list(鏈表結(jié)構(gòu))
Methods connected with 'add' & 'remove' methods are used fequently.

-|Set (A Collection)

Unordered(無序的)
Duplicate objects are not allowed.
HashSet method is used frequently.


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末醉拓,一起剝皮案震驚了整個(gè)濱河市伟姐,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌廉嚼,老刑警劉巖玫镐,帶你破解...
    沈念sama閱讀 222,729評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異怠噪,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)杜跷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,226評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門傍念,熙熙樓的掌柜王于貴愁眉苦臉地迎上來矫夷,“玉大人,你說我怎么就攤上這事憋槐∷海” “怎么了?”我有些...
    開封第一講書人閱讀 169,461評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵阳仔,是天一觀的道長忧陪。 經(jīng)常有香客問我,道長近范,這世上最難降的妖魔是什么嘶摊? 我笑而不...
    開封第一講書人閱讀 60,135評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮评矩,結(jié)果婚禮上叶堆,老公的妹妹穿的比我還像新娘。我一直安慰自己斥杜,他們只是感情好虱颗,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,130評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蔗喂,像睡著了一般忘渔。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上缰儿,一...
    開封第一講書人閱讀 52,736評(píng)論 1 312
  • 那天畦粮,我揣著相機(jī)與錄音,去河邊找鬼返弹。 笑死锈玉,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的义起。 我是一名探鬼主播拉背,決...
    沈念sama閱讀 41,179評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼默终!你這毒婦竟也來了椅棺?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,124評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤齐蔽,失蹤者是張志新(化名)和其女友劉穎两疚,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體含滴,經(jīng)...
    沈念sama閱讀 46,657評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡诱渤,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,723評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了谈况。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片勺美。...
    茶點(diǎn)故事閱讀 40,872評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡递胧,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出赡茸,到底是詐尸還是另有隱情缎脾,我是刑警寧澤,帶...
    沈念sama閱讀 36,533評(píng)論 5 351
  • 正文 年R本政府宣布占卧,位于F島的核電站遗菠,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏华蜒。R本人自食惡果不足惜辙纬,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,213評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望友多。 院中可真熱鬧牲平,春花似錦、人聲如沸域滥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,700評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽启绰。三九已至昂儒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間委可,已是汗流浹背渊跋。 一陣腳步聲響...
    開封第一講書人閱讀 33,819評(píng)論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留着倾,地道東北人拾酝。 一個(gè)月前我還...
    沈念sama閱讀 49,304評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像卡者,于是被迫代替她去往敵國和親蒿囤。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,876評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容