今天一时心痒,想练习一下关于对象排序的方法,于是,就有了这篇博客,敬请欣赏:
package com.deepfounder.api.sort;
public class ObjectSort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ObjectSort os=new ObjectSort();
User us=new User();
java.util.ArrayList<User> al= os.Sort(us.getUserList(300));
for(int i=0;i<al.size();i++)
{
System.out.println(((User)(al.get(i))).getUserName());
}
}
public java.util.ArrayList<User> Sort(java.util.ArrayList<User> al)
{
User Temp=new User();
//Temp=new User();
for(int i=0;i<al.size();i++)
{
for(int j=0;j<al.size();j++)
{
if(((User)(al.get(i))).getUserID()>((User)(al.get(j))).getUserID())
{
Temp=al.get(i);
al.add(i, al.get(j));
al.add(j,Temp);
}
}
}
return al;
}
}
package com.deepfounder.api.sort;
public class User
{
private int UserID=0;
public int getUserID() {
return UserID;
}
public void setUserID(int userID) {
UserID = userID;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
private String UserName="";
public static java.util.ArrayList<User> getUserList(int T_Index)
{
java.util.ArrayList<User> usl=new java.util.ArrayList<User>();
for(int index=0;index<T_Index;index++)
{
User us=new User();
us.setUserID((int)(java.lang.Math.random())*T_Index);
us.setUserName("UserName:"+index);
usl.add(us);
//us.Free();
}
return usl;
}
public void Free()
{
this.UserName=null;
}
}
Ok,Hello World!