ArrayList存放學(xué)生對(duì)象及排序

1)創(chuàng)建一個(gè)ArrayList對(duì)象list,將上述三位學(xué)生加入到集合中,并遍歷list,顯示學(xué)生信息;

2)在學(xué)生 “l(fā)iu”的前面插入一個(gè)學(xué)生,學(xué)生的信息如下:("li",1409,16,"english")并再一次遍歷list,確認(rèn)學(xué)生“l(fā)i”已經(jīng)被正確插入。

3)根據(jù)學(xué)生的年齡,按照年齡從小到大的順序?qū)@4位學(xué)生進(jìn)行排序,并再次遍歷,顯示學(xué)生信息。

package ldz;

import java.util.*;

public class ArrayList1 {
    

    public static void main(String[] args) {
        List<Student> c = new ArrayList<Student>();
        Student s1=new Student("zhang",1401, 20, "computer");
        Student s2=new Student("liu",1402,19,"law");
        Student s3=new Student("wang",1403,17,"mechanical");
        c.add(s1);
        c.add(s2);
        c.add(s3);
        
        Iterator<Student> it = c.iterator();
        while (it.hasNext()) {
            Student s = it.next();
            System.out.println(s);
            System.out.println();
        }
        
        Student s4=new Student("li",1409,16,"english");
        c.add(1,s4);
        
        Iterator<Student> it1 = c.iterator();
        while (it1.hasNext()) {
            Student s = it1.next();
            System.out.println(s);
            System.out.println();
        }
        
        
//        Collections.sort(c);                  //11111
//        Collections.sort(c,new Outcompare()); //222222
        Collections.sort(c,new Comparator<Student>(){

            /*@Override
            public int compare(Object o1, Object o2) {
                Student w1 = (Student)o1;
                Student w2 = (Student)o2;
                return w1.getAge()-w2.getAge();
            }*/

            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge()-o2.getAge();
            }});
        Iterator<Student> it2 = c.iterator();
        while (it2.hasNext()) {
            Student s = it2.next();
            System.out.println(s);
            System.out.println();
        }
        

        
        
    }
    
    
                                            
}
package ldz;

import java.util.Comparator;

public class Outcompare implements Comparator{

    @Override
    public int compare(Object o1, Object o2) {
        Student w1 = (Student)o1;
        Student w2 = (Student)o2;
        return w1.getAge()-w2.getAge();
    }

}  
package ldz;

public class Student implements Comparable {
    private String name;
    private int id;
    private int age;
    private String major;

    public Student(String name, int id, int age, String major) {
        super();
        this.name = name;
        this.id = id;
        this.age = age;
        this.major = major;
    }


    public int getAge() {
        return age;
    }


    public String toString() {

        return "Student [name=" + name + ", id=" + id + ", age=" + age + ", major=" + major + "]";

    }


    @Override
    public int compareTo(Object o) {
        Student s = (Student)o;
        return this.age-s.getAge();
    }
    

}

?著作權(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)容

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