新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
怎么在java中利用ArrayList根據(jù)存儲對象-創(chuàng)新互聯(lián)
這篇文章將為大家詳細(xì)講解有關(guān)怎么在java中利用ArrayList根據(jù)存儲對象,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
具體如下:
package demo; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; class Stu{ public int age; private String name; public Stu(String name,int age){ this.age=age; this.name=name; } public String toString(){ String str=name+"is "+age; return str; } } public class Sort{ @SuppressWarnings({ "unchecked", "rawtypes" }) public static void main(String[] args){ ArrayLists=new ArrayList (); s.add(new Stu("cjc",25)); s.add(new Stu("jake",17)); s.add(new Stu("john",30)); s.add(new Stu("Alice",23)); System.out.println("創(chuàng)新互聯(lián)測試結(jié)果:"); System.out.println("The order before sorting!"); System.out.println(s); //對集合中的元素更具年齡進(jìn)行排序 Collections.sort(s,new Comparator(){ @Override public int compare(Object o1, Object o2) { // TODO Auto-generated method stub Stu stu1=(Stu)o1; Stu stu2=(Stu)o2; return stu2.age-stu1.age; } }); System.out.println("The order after sorting!"); System.out.println(s); } }
關(guān)于怎么在java中利用ArrayList根據(jù)存儲對象就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
分享文章:怎么在java中利用ArrayList根據(jù)存儲對象-創(chuàng)新互聯(lián)
URL分享:http://fisionsoft.com.cn/article/iijje.html