public class test1 {
private int id;
private String name;
public test1(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String args[]){
List<test1> list = new ArrayList<test1>();
list.add(new test1(1,"a"));
list.add(new test1(1,"ab"));
list.add(new test1(2,"ac"));
list.add(new test1(2,"ad"));
list.add(new test1(3,"aadsa"));
list.add(new test1(3,"aasd"));
list.add(new test1(4,"aasd"));
list.add(new test1(4,"aw"));
list.add(new test1(3,"ar"));
}
} 我怎么能够排列成id 是 1,2,3,4,1,2,3,4,3这样子呢
private int id;
private String name;
public test1(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String args[]){
List<test1> list = new ArrayList<test1>();
list.add(new test1(1,"a"));
list.add(new test1(1,"ab"));
list.add(new test1(2,"ac"));
list.add(new test1(2,"ad"));
list.add(new test1(3,"aadsa"));
list.add(new test1(3,"aasd"));
list.add(new test1(4,"aasd"));
list.add(new test1(4,"aw"));
list.add(new test1(3,"ar"));
}
} 我怎么能够排列成id 是 1,2,3,4,1,2,3,4,3这样子呢
list.add(new test1(2,"ac"));
list.add(new test1(3,"aadsa"));
list.add(new test1(4,"aasd"));
list.add(new test1(1,"ab"));
list.add(new test1(2,"ad"));
list.add(new test1(3,"aasd"));
list.add(new test1(4,"aw"));
其实list的集合就是有序的,会按你的存储顺序进行排列的,先放的就排在前面
2.while(dataList.size > 0),执行3
3.建立临时list(tempList),执行4
4.遍历dataList,如果和tempList中有不同的加入到tempList中然后从数组中删除,执行5
5.如果size == 0,return,否则执行2
可能不是楼主的愿意,仅供参考一下。import java.util.List;
import java.util.ArrayList;
import java.util.Collections; //用于排序
public class MySort
{
public static void main(String args[])
{
List<Test1> list = new ArrayList<Test1>();
list.add(new Test1(1,"a"));
list.add(new Test1(1,"ab"));
list.add(new Test1(2,"ac"));
list.add(new Test1(2,"ad"));
list.add(new Test1(3,"aadsa"));
list.add(new Test1(3,"aasd"));
list.add(new Test1(4,"aasd"));
list.add(new Test1(4,"aw"));
list.add(new Test1(3,"ar")); Collections.sort(list); //排序。112233344
List<Test1> list2=sort(list); //按 1234 1234 3 顺序排。
for(Test1 t:list2) //输出结果。
{
System.out.println(t);
}
}
//排序输出,1234,1234,3
//要求,list经过排序。
public static List<Test1> sort(List<Test1> list)
{
List<Test1> listNew=new ArrayList<Test1>(); //放排好序的列表.
int i=0; //用于新列表的位置计数.
int index=0; //原列表的位置计数.
listNew.add(list.get(0)); //先放第一个.
list.remove(0); //从原列表里删除第一个. while(list.size()>0)
{
//第一种情况,新列表当前位置元素"大于"原列表当前位置的元素。
//
if(listNew.get(i).getId() > list.get(index).getId())
{
if(index==0) //如果这个元素是第一个,放到新列表里。
{
listNew.add(list.get(index));
i++; //计数加一。
list.remove(index); //删除这个元素。
}
else //否则,跳过
{
index++;
if(index==list.size()) //原列表到了最后,使其指向第0个元素。
{
index=0;
}
}
}
//第二种情况,新列表元素"小于"原列表,则将原列表元素放到新表里。
//
else if(listNew.get(i).getId() < list.get(index).getId())
{
listNew.add(list.get(index));
i++;
list.remove(index);
if(index==list.size())
{
index=0;
}
}
//第三种情况,新列表元素"等于"原列表,跳过
//
else if(listNew.get(i).getId() == list.get(index).getId())
{
index++;
if(index==list.size())
{
index=0;
}
} }//end while.
return listNew;
}//end method sort().
}//end class MySort.class Test1 implements Comparable<Test1>
{
private int id;
private String name;
public Test1(int id, String name)
{
super();
this.id = id;
this.name = name;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String toString()
{
return id+" "+name;
}
//对象比较。id相同,比较其name属性,name 属性按String的规则比较。
//
public int compareTo(Test1 t)
{
if(this.getId()>t.getId())
{
return 1;
}
else if(this.getId()<t.getId())
{
return -1;
}
else
{
return this.getName().compareTo(t.getName());
}
}
}//end class Test1