问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

如何对指针的list列表进行排序

发布网友 发布时间:2022-05-07 07:16

我来回答

1个回答

热心网友 时间:2023-10-20 14:02

[size=large][color=red]要对List排序,你要对List里装的这种类型的类实现排序接口(Comparable).
举个例子:

下面这个例子是对List<MyObject>进行排序.使用Collections.sort(List list);方法,此方法会调用MyObject的compareTo方法.所以在MyObject类定义里要实现compareTo方法.

Java代码
public class ListSort {

/**
* main()
* 2010-4-2,下午09:25:57
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<MyObject> lists = new ArrayList<MyObject>();
MyObject obj1 = new MyObject("d");
MyObject obj2 = new MyObject("a");
lists.add(obj1);
lists.add(obj2);
Collections.sort(lists);
for (MyObject myObject : lists) {
System.out.println(myObject.getContent());
}
}

}
class MyObject implements Comparable<MyObject>{
private String content;
public MyObject(String _content) {
this.content = _content;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public int compareTo(MyObject obj) {
// TODO Auto-generated method stub
if (null == obj) return 1;
else {
return this.content.compareTo(obj.content);
}

}

}

下面是Collections.sort方法

Java代码
public static <T extends Comparable<? super T>> void sort(List<T> list) {
Object[] a = list.toArray();
Arrays.sort(a);
ListIterator<T> i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set((T)a[j]);
}
}

实际上我们的MyObject类的方法compareTo是在Arrays.sort()中被调用的...
请看..

Java代码
public static void sort(Object[] a) {
Object[] aux = (Object[])a.clone();
mergeSort(aux, a, 0, a.length, 0);
}

Java代码
private static void mergeSort(Object[] src,
Object[] dest,
int low,
int high,
int off) {
int length = high - low;

// Insertion sort on smallest arrays
if (length < INSERTIONSORT_THRESHOLD) {
for (int i=low; i<high; i++)
for (int j=i; j>low &&
((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
swap(dest, j, j-1);
return;
}

// Recursively sort halves of dest into src
int destLow = low;
int destHigh = high;
low += off;
high += off;
int mid = (low + high) >> 1;
mergeSort(dest, src, low, mid, -off);
mergeSort(dest, src, mid, high, -off);

// If list is already sorted, just copy from src to dest. This is an
// optimization that results in faster sorts for nearly ordered lists.
if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
System.arraycopy(src, low, dest, destLow, length);
return;
}

// Merge sorted halves (now in src) into dest
for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
dest[i] = src[p++];
else
dest[i] = src[q++];
}
}
[/color][/size]

当然,这是一种实现排序的办法.还有一种是实现Comparator,实现这个接口,然后使用
Collections.sort(List list,Comparator c);这个方法来排序..

热心网友 时间:2023-10-20 14:02

[size=large][color=red]要对List排序,你要对List里装的这种类型的类实现排序接口(Comparable).
举个例子:

下面这个例子是对List<MyObject>进行排序.使用Collections.sort(List list);方法,此方法会调用MyObject的compareTo方法.所以在MyObject类定义里要实现compareTo方法.

Java代码
public class ListSort {

/**
* main()
* 2010-4-2,下午09:25:57
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<MyObject> lists = new ArrayList<MyObject>();
MyObject obj1 = new MyObject("d");
MyObject obj2 = new MyObject("a");
lists.add(obj1);
lists.add(obj2);
Collections.sort(lists);
for (MyObject myObject : lists) {
System.out.println(myObject.getContent());
}
}

}
class MyObject implements Comparable<MyObject>{
private String content;
public MyObject(String _content) {
this.content = _content;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public int compareTo(MyObject obj) {
// TODO Auto-generated method stub
if (null == obj) return 1;
else {
return this.content.compareTo(obj.content);
}

}

}

下面是Collections.sort方法

Java代码
public static <T extends Comparable<? super T>> void sort(List<T> list) {
Object[] a = list.toArray();
Arrays.sort(a);
ListIterator<T> i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set((T)a[j]);
}
}

实际上我们的MyObject类的方法compareTo是在Arrays.sort()中被调用的...
请看..

Java代码
public static void sort(Object[] a) {
Object[] aux = (Object[])a.clone();
mergeSort(aux, a, 0, a.length, 0);
}

Java代码
private static void mergeSort(Object[] src,
Object[] dest,
int low,
int high,
int off) {
int length = high - low;

// Insertion sort on smallest arrays
if (length < INSERTIONSORT_THRESHOLD) {
for (int i=low; i<high; i++)
for (int j=i; j>low &&
((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
swap(dest, j, j-1);
return;
}

// Recursively sort halves of dest into src
int destLow = low;
int destHigh = high;
low += off;
high += off;
int mid = (low + high) >> 1;
mergeSort(dest, src, low, mid, -off);
mergeSort(dest, src, mid, high, -off);

// If list is already sorted, just copy from src to dest. This is an
// optimization that results in faster sorts for nearly ordered lists.
if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
System.arraycopy(src, low, dest, destLow, length);
return;
}

// Merge sorted halves (now in src) into dest
for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
dest[i] = src[p++];
else
dest[i] = src[q++];
}
}
[/color][/size]

当然,这是一种实现排序的办法.还有一种是实现Comparator,实现这个接口,然后使用
Collections.sort(List list,Comparator c);这个方法来排序..

热心网友 时间:2023-10-20 14:02

[size=large][color=red]要对List排序,你要对List里装的这种类型的类实现排序接口(Comparable).
举个例子:

下面这个例子是对List<MyObject>进行排序.使用Collections.sort(List list);方法,此方法会调用MyObject的compareTo方法.所以在MyObject类定义里要实现compareTo方法.

Java代码
public class ListSort {

/**
* main()
* 2010-4-2,下午09:25:57
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<MyObject> lists = new ArrayList<MyObject>();
MyObject obj1 = new MyObject("d");
MyObject obj2 = new MyObject("a");
lists.add(obj1);
lists.add(obj2);
Collections.sort(lists);
for (MyObject myObject : lists) {
System.out.println(myObject.getContent());
}
}

}
class MyObject implements Comparable<MyObject>{
private String content;
public MyObject(String _content) {
this.content = _content;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public int compareTo(MyObject obj) {
// TODO Auto-generated method stub
if (null == obj) return 1;
else {
return this.content.compareTo(obj.content);
}

}

}

下面是Collections.sort方法

Java代码
public static <T extends Comparable<? super T>> void sort(List<T> list) {
Object[] a = list.toArray();
Arrays.sort(a);
ListIterator<T> i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set((T)a[j]);
}
}

实际上我们的MyObject类的方法compareTo是在Arrays.sort()中被调用的...
请看..

Java代码
public static void sort(Object[] a) {
Object[] aux = (Object[])a.clone();
mergeSort(aux, a, 0, a.length, 0);
}

Java代码
private static void mergeSort(Object[] src,
Object[] dest,
int low,
int high,
int off) {
int length = high - low;

// Insertion sort on smallest arrays
if (length < INSERTIONSORT_THRESHOLD) {
for (int i=low; i<high; i++)
for (int j=i; j>low &&
((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
swap(dest, j, j-1);
return;
}

// Recursively sort halves of dest into src
int destLow = low;
int destHigh = high;
low += off;
high += off;
int mid = (low + high) >> 1;
mergeSort(dest, src, low, mid, -off);
mergeSort(dest, src, mid, high, -off);

// If list is already sorted, just copy from src to dest. This is an
// optimization that results in faster sorts for nearly ordered lists.
if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
System.arraycopy(src, low, dest, destLow, length);
return;
}

// Merge sorted halves (now in src) into dest
for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
dest[i] = src[p++];
else
dest[i] = src[q++];
}
}
[/color][/size]

当然,这是一种实现排序的办法.还有一种是实现Comparator,实现这个接口,然后使用
Collections.sort(List list,Comparator c);这个方法来排序..
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
分手后说过的话精选185句 分手说的精选113句 分手怎么挽回女朋友的句子精选162句 现在工作忙的要死,女朋友因为这跟我分手,我该怎么办? ...任意组成两个不同的两位小数,并把它们改写成以0.001为计数单位... m3/MW▪h怎么换算m3/s▪GW? 交通银行的刷卡金怎么使用呢? 什么时候刷卡金 ...突然前面两条腿好像断了一样 走路前面两腿都无法支撑 吃饭就坐着吃... 王者荣耀:伤害与回复兼备的杨戬,为何不受待见,真废了吗? C语言使用指针实现选择法排序。 C语言 指针 排序法 c语言 指针 字符串 排序 如何将指针中的数排序,见如下程序,菜鸟求高手解答 用指针实现排序 php和mysql数据库问题求大神解答 沿换个偏旁组成新字并组词? “铅”“超”“钱”“集”“投”字换偏旁组成新字,最好每个组3个新字,谢谢。 把铅笔的偏旁换一个偏旁,改成新字? 小米6,华为p10等几款国产双摄手机,哪款最强 除了中华听书网,还可以在哪里听小说? 好看听书网 有什么好听的小说呢? 玄幻的 如何增大肌肉让肌肉变大的方法 怎么才能最快的增长肌肉块! 增大肌肉的方法 快速增长肌肉的方法 什么时间肌肉增长最快 怎样把酷狗音乐文件夹转移到其他盘里 怎么把酷狗音乐复制在移动硬盘上? 语文应试技巧或者方法? 三校生面试方法技巧? 如何用指针法对十个数排序? c语言如何用指针排列字符 梦见情人拉着我的手去唱歌 c语言用指针写选择法排序函数 c语言用指针写选择排序法 梦见情人牵自己的手,但是很快放下了 用指针实现三个整数的排序·谢谢! 指针数组排序 将数组指针排序 梦见旧情人拉着我的手跑,我们俩都恨高兴,忽然从山上摔下来 梦见情人醒来拉着我的手 C语言指针排序,运行输入数据后程序停止运行 梦到一个人,现实中根本不认识,但梦中的他牵着我的手走过了很多奇奇怪怪甚至恐怖的地方,给我很大的安全 C语言指针实现整数排序 梦见别人牵自己的手,代表什么? C语言使用指针对字符排序 采用指针对数组进行排序c语言指针 c语言指针数组排序? 操作圆盘锯应符合哪些安全要求? 威克士电圆锯靠山怎么用