java的排序函數
㈠ java數組排序
import java.util.Arrays;
import java.util.Scanner;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Object[] obj=null;
int[] l=null;
int m=0;
for(int i=0;i<10;i++){
People people = new People();
people.setHeight(i);
obj[i]=people;
}
for(Object k:obj){
l[m] =(People)k.getHeight;
m++;
}
Arrays.sort(l);
}
}
不知道你說的是不是這個意思。。。。
㈡ java 配列排序
按照你的思路把程序改了一下,你看看是不是你想要的結果。程序如下:
public class A {
private static void selectionSort(int a[], int n) {
//臨時變數 存放較小的值
int min;
//排序
for (int i = 0; i < n; i++) {
for (int j = (n-1); j > i; j--) {
if (a[j] < a[j - 1]) {
min = a[j];
a[j] = a[j - 1];
a[j - 1] = min;
}
}
}
//輸出
for (int i = 0; i < n; i++) {
System.out.print(a[i]+" ");
}
//換行
System.out.println();
}
public static void main(String[] args) {
int a[] = { 12,5,10,15,9,2 };
int b[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
int c[] = { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
selectionSort(a, a.length);
selectionSort(b, b.length);
selectionSort(c, c.length);
}
}
㈢ 求一個用JAVA函數 對數組排序的程序。
import java.util.Arrays;
import java.util.Scanner;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Object[] obj=null;
int[] l=null;
int m=0;
for(int i=0;i<10;i++){
People people = new People();
people.setHeight(i);
obj[i]=people;
}
for(Object k:obj){
l[m] =(People)k.getHeight;
m++;
}
Arrays.sort(l);
}
}
一維數組的
㈣ java自動排序函數.......
public static void sort(int[] a,
int fromIndex,
int toIndex)對指定 int 型數組的指定范圍按數字升序進行排序。排序的范圍從索引回 fromIndex(包括)一答直到索引 toIndex(不包括)。(如果 fromIndex==toIndex,則排序范圍為空。)
該排序演算法是一個經過調優的快速排序法,改編自 Jon L. Bentley 和 M. Douglas McIlroy 合著的 Engineering a Sort Function", Software-Practice and Experience Vol. 23(11) P. 1249-1265 (November 1993)。此演算法在許多數據集上提供 n*log(n) 性能,這導致其他快速排序會降低二次型性能。
參數:
a - 要排序的數組
fromIndex - 要排序的第一個元素的索引(包括)
toIndex - 要排序的最後一個元素的索引(不包括)
㈤ java數組sort()方法降序怎麼排
看了一下Arrays這個類
貌似沒有降序排序這個方法,Collections這個類里有個方法可以實現
public
class
TestToArray
{
public
static
void
main(String[]
args)
{
//int[]
height
=
{1,2,3,4,5,}
ArrayList<Integer>
list2=new
ArrayList<Integer>();
for(int
i=1;i<=5;i++)
{
list2.add(i);
}
Collections.reverse(list2);
for(int
i=0;
i<list2.size();
i++)
System.out.println(list2.get(i));
}
}
運行內結果:
C:\Users\ZhangJun\Desktop\桌面文檔\TestMethod>java
TestToArray
5
4
3
2
1
不要意容思
我這個只是實現了翻轉順序
並未排序
但是用Collections那個類的一些方法應該就能實現
稍後給你寫
㈥ java 函數按照什麼順序排列
最簡單的方法是:
你調用發送驗證碼方法的時候 set.nowTime發送時當前的時間 set.userid
驗證碼比較的時候 把nowTime 與當前時間比較一下 看是否在有效時間內
㈦ java中最簡單的方法冒泡排序
package bubble_sort;
import java.util.Scanner;
public class Sort {
/**
* 冒泡排序
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("請輸入要排序的個數:");
int counts = input.nextInt();
int[] temp = new int[counts];
for (int i = 0; i < counts; i++) {// 賦值
System.out.println("請輸入第" + (i + 1) + "數:");
temp[i] = input.nextInt();
}
for(int k=0, conversion=0;k<counts;k++){//降序
for(int j=0;j<(counts-1);j++){
if(temp[j]>temp[j+1]){
conversion=temp[j];
temp[j]=temp[j+1];
temp[j+1]=conversion;
}
}
}
System.out.println("降序後");
for(int g=0;g<counts;g++){//輸出
System.out.println(temp[g]);
}
for(int k=0, conversion=0;k<counts;k++){//升序
for(int j=0;j<(counts-1);j++){
if(temp[j]<temp[j+1]){
conversion=temp[j];
temp[j]=temp[j+1];
temp[j+1]=conversion;
}
}
}
System.out.println("升序後");
for(int g=0;g<counts;g++){//輸出
System.out.println(temp[g]);
}
String t=input.next();//為了運行結束後查看結果,進行停留
}
}
㈧ Java類的設計,排序函數的設計
總歸是要存這個 課程名 - 成績 數據結構的,這里我用Map。
classCourse{
finalpublicstaticStringYUWEN="YuWen";
finalpublicstaticStringSHUXUE="ShuXue";
finalpublicstaticStringYINGYU="YingYu";
}
classStudent{
publicintid;//學號
publicStringname;//姓名
publicMap<String,Integer>courseScore;//課程名-成績
publicaddScore(Stringcourse,intscore){
this.courseScore.put(course,score);
}
//...
}
publicclassTest{
publicstaticvoidsortStudent(List<Student>ss,StringsortKey){
Collections.sort(ss,newComparator(){
@Override
publicintcompare(Studentarg0,Studentarg1){
ints0=arg0.courseScore.get(sortKey);
ints1=arg1.courseScore.get(sortKey);
returns0-s1;
}
});
}
publicstaticvoidmain(String[]args){
List<Student>ss=newArrayList<Student>();
Studentx=newStudent();
Student.addScore(Course.YUWEN,100);
Student.addScore(Course.SHUXUE,100);
Student.addScore(Course.YINGYU,100);
//...
try{
sortStudent(ss,Course.YUWEN);
}catch{//nullPointerException
;
}
}
}
大致是這樣。為了防止亂取課程名,在使用的時候必須用Course類里的常量。其實改成Enum更恰當一些。
另外還有些介面函數我省略沒寫,偷懶把很多數據都public了。
㈨ java中有沒有直接將整型數組中的元素進行排序的方法
類 Arrays
public static void sort(int[] a)對指定的 int 型數組按數字升序進行排序。該排序演算法是一個經過調優回的快速排序法,改編自 Jon L. Bentley 和 M. Douglas McIlroy 合著答的 Engineering a Sort Function", Software-Practice and Experience Vol. 23(11) P. 1249-1265 (November 1993)。此演算法在許多數據集上提供 n*log(n) 性能,這導致其他快速排序會降低二次型性能。
參數:
a - 要排序的數組
你是什麼api啊,我都是從上面復制來,你來個api里沒有。。
java.util.Arrays