} }class A{ int index; public A(int i){ index = i; }
public String toString(){ return String.valueOf(index); } }class Cmp implements Comparator{ public int compare(Object a, Object b){ int i = ((A)a).index; int j = ((A)b).index; return (i<j ? -1 : (i>j ? 1 : 0)); } }
import java.util.*; public class A { public int index; public A(int i){ index = i; } public String toString(){ return this.index + ""; } public static void main(String[] args){ Random rand = new Random(); List lists = new ArrayList(); List lists1 = new ArrayList(); List lists2 = new ArrayList(); for(int i=0;i<4;i++){ lists.add(new A(rand.nextInt(10))); lists1.add(new A(rand.nextInt(5))); } for(int k=0;k<lists.size();k++) lists2.add(lists.get(k)); for(int k=0;k<lists1.size();k++) lists2.add(lists.get(k)); System.out.println("before sort"); System.out.println(lists2); Compare a = new Compare(); Collections.sort(lists2,a); System.out.println("after sort"); System.out.println(lists2); } } class Compare implements Comparator{ public int compare(Object a, Object b){ int i = ((A)a).index; int j = ((A)b).index; return (i<j ? -1 : (i==j ? 0 : 1)); } } ^_^,不习惯用import java.util.*; public class A { public int index; public A(int i){ index = i; } public String toString(){ return this.index + ""; } public static void main(String[] args){ Random rand = new Random(); List lists = new ArrayList(); List lists1 = new ArrayList(); List lists2 = new ArrayList(); for(int i=0;i<4;i++) lists.add(new A(rand.nextInt(10))); for(int j=0;j<3;j++) lists1.add(new A(rand.nextInt(5))); for(int k=0;k<lists.size();k++) lists2.add(lists.get(k)); for(int k=0;k<lists1.size();k++) lists2.add(lists.get(k)); System.out.println("before sort"); System.out.println(lists2); Compare a = new Compare(); Collections.sort(lists2,a); System.out.println("after sort"); System.out.println(lists2); } } class Compare implements Comparator{ public int compare(Object a, Object b){ int i = ((A)a).index; int j = ((A)b).index; return (i<j ? -1 : (i==j ? 0 : 1)); } } 不习惯用Vector,所以用 ArrayList.
^_^ ,干脆这样把 用Comparable吧. import java.util.*; public class A implements Comparable{ public int index; public A(int i){ index = i; } public int compareTo(Object rv){ int rvi = ((A)rv).index; return (index<rvi ? -1 : (index==rvi ? 0 : 1)); } public String toString(){ return this.index + ""; } public static void main(String[] args){ Random rand = new Random(); List lists = new ArrayList(); List lists1 = new ArrayList(); List lists2 = new ArrayList(); for(int i=0;i<4;i++){ lists.add(new A(rand.nextInt(10))); lists1.add(new A(rand.nextInt(5))); } for(int k=0;k<lists.size();k++) lists2.add(lists.get(k));
^_^ 楼主,给你个建议,去把thinking in java好好的看一遍吧,真的,你问的这些问题里面都有讲.使用内置的排序方法,就可以对任意的基本类型数组排序;也可以对任意的对象数组进行排序,只要该对象实现了Comparable接口或具有相关联的 Comparatpr.上面说得是对数组排序, 然后你可以翻一下jdk文档. Collections有2个方法: 1 sort public static <T extends Comparable<? super T>> void sort(List<T> list)Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list). This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.The specified list must be modifiable, but need not be resizable.The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place. Parameters: list - the list to be sorted. Throws: ClassCastException - if the list contains elements that are not mutually comparable (for example, strings and integers). UnsupportedOperationException - if the specified list's list-iterator does not support the set operation. See Also: Comparable2 sort public static <T> void sort(List<T> list, Comparator<? super T> c) Sorts the specified list according to the order induced by the specified comparator. All elements in the list must be mutually comparable using the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the list). This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. The specified list must be modifiable, but need not be resizable. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place. Parameters: list - the list to be sorted. c - the comparator to determine the order of the list. A null value indicates that the elements' natural ordering should be used. Throws: ClassCastException - if the list contains elements that are not mutually comparable using the specified comparator. UnsupportedOperationException - if the specified list's list-iterator does not support the set operation. See Also: Comparator 自己去看文档吧!
public static void main(String args[]){
Vector <A>v1 = new Vector<A>();
Vector <A>v2 = new Vector<A>();
Random rnd = new Random(10);
for(int i=0; i<5; i++){
v1.add(new A(rnd.nextInt()));
v2.add(new A(rnd.nextInt()));
}
A[]array = new A[v1.size() + v2.size()];
int i=0;
for(; i<v1.size(); i++){
array[i] = v1.get(i);
}
for(int j=0; j<v2.size(); j++){
array[i+j] = v2.get(j);
}
Arrays.sort(array,new Cmp()); for(i=0; i<array.length; i++){
System.out.println(array[i]);
}
}
}class A{
int index;
public A(int i){
index = i;
}
public String toString(){
return String.valueOf(index);
}
}class Cmp implements Comparator{
public int compare(Object a, Object b){
int i = ((A)a).index;
int j = ((A)b).index;
return (i<j ? -1 : (i>j ? 1 : 0));
}
}
public class A {
public int index;
public A(int i){
index = i;
}
public String toString(){
return this.index + "";
}
public static void main(String[] args){
Random rand = new Random();
List lists = new ArrayList();
List lists1 = new ArrayList();
List lists2 = new ArrayList();
for(int i=0;i<4;i++){
lists.add(new A(rand.nextInt(10)));
lists1.add(new A(rand.nextInt(5)));
}
for(int k=0;k<lists.size();k++)
lists2.add(lists.get(k));
for(int k=0;k<lists1.size();k++)
lists2.add(lists.get(k));
System.out.println("before sort");
System.out.println(lists2);
Compare a = new Compare();
Collections.sort(lists2,a);
System.out.println("after sort");
System.out.println(lists2);
}
}
class Compare implements Comparator{
public int compare(Object a, Object b){
int i = ((A)a).index;
int j = ((A)b).index;
return (i<j ? -1 : (i==j ? 0 : 1));
}
}
^_^,不习惯用import java.util.*;
public class A {
public int index;
public A(int i){
index = i;
}
public String toString(){
return this.index + "";
}
public static void main(String[] args){
Random rand = new Random();
List lists = new ArrayList();
List lists1 = new ArrayList();
List lists2 = new ArrayList();
for(int i=0;i<4;i++)
lists.add(new A(rand.nextInt(10)));
for(int j=0;j<3;j++)
lists1.add(new A(rand.nextInt(5)));
for(int k=0;k<lists.size();k++)
lists2.add(lists.get(k));
for(int k=0;k<lists1.size();k++)
lists2.add(lists.get(k));
System.out.println("before sort");
System.out.println(lists2);
Compare a = new Compare();
Collections.sort(lists2,a);
System.out.println("after sort");
System.out.println(lists2);
}
}
class Compare implements Comparator{
public int compare(Object a, Object b){
int i = ((A)a).index;
int j = ((A)b).index;
return (i<j ? -1 : (i==j ? 0 : 1));
}
}
不习惯用Vector,所以用 ArrayList.
import java.util.*;
public class A implements Comparable{
public int index;
public A(int i){
index = i;
}
public int compareTo(Object rv){
int rvi = ((A)rv).index;
return (index<rvi ? -1 : (index==rvi ? 0 : 1));
}
public String toString(){
return this.index + "";
}
public static void main(String[] args){
Random rand = new Random();
List lists = new ArrayList();
List lists1 = new ArrayList();
List lists2 = new ArrayList();
for(int i=0;i<4;i++){
lists.add(new A(rand.nextInt(10)));
lists1.add(new A(rand.nextInt(5)));
}
for(int k=0;k<lists.size();k++)
lists2.add(lists.get(k));
for(int k=0;k<lists1.size();k++)
lists2.add(lists1.get(k));
System.out.println("before sort");
System.out.println(lists2);
Collections.sort(lists2);
System.out.println("after sort");
System.out.println(lists2);
}
}
及相关方法
我不能边看边猜啊,JAVA的类真是多啊
implements是java的关键字~~也就是实现的意思
Collections有2个方法:
1
sort
public static <T extends Comparable<? super T>> void sort(List<T> list)Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list).
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.The specified list must be modifiable, but need not be resizable.The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place.
Parameters:
list - the list to be sorted.
Throws:
ClassCastException - if the list contains elements that are not mutually comparable (for example, strings and integers).
UnsupportedOperationException - if the specified list's list-iterator does not support the set operation.
See Also:
Comparable2
sort
public static <T> void sort(List<T> list,
Comparator<? super T> c)
Sorts the specified list according to the order induced by the specified comparator. All elements in the list must be mutually comparable using the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the list).
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. The specified list must be modifiable, but need not be resizable. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place.
Parameters:
list - the list to be sorted.
c - the comparator to determine the order of the list. A null value indicates that the elements' natural ordering should be used.
Throws:
ClassCastException - if the list contains elements that are not mutually comparable using the specified comparator.
UnsupportedOperationException - if the specified list's list-iterator does not support the set operation.
See Also:
Comparator
自己去看文档吧!