標題:
對陣列排序 (一)
[打印本頁]
作者:
tonyh
時間:
2019-1-24 14:16
標題:
對陣列排序 (一)
本帖最後由 tonyh 於 2019-7-4 14:11 編輯
假設有一陣列其內容為:
int n[]={34,65,7,-3,6546,-564,0,-11,90,123};
試運用 Arrays 類別中的 sort() 函式, 對該陣列排序.
[attach]6783[/attach]
import java.util.Arrays;
public class Ch42
{
public static void main(String args[])
{
int n[]={34,65,7,-3,6546,-564,0,-11,90,123};
System.out.println("排序前:");
for(int i=0; i<n.length; i++)
System.out.print(n[i]+" ");
System.out.println();
Arrays.sort(n);
System.out.println("排序後:");
for(int i=0; i<n.length; i++)
System.out.print(n[i]+" ");
}
}
複製代碼
import java.util.Arrays;
public class Ch60 {
public static void main(String[] args){
int n[]={34,65,7,-3,6546,-564,0,-11,90,123};
System.out.println("排序前:");
for(int i=0; i<n.length; i++)
System.out.print(n[i]+" ");
System.out.println();
Arrays.sort(n);
System.out.println("排序後(遞增):");
for(int i=0; i<n.length; i++)
System.out.print(n[i]+" ");
System.out.println();
System.out.println("排序後(遞減):");
for(int i=n.length-1; i>=0; i--)
System.out.print(n[i]+" ");
System.out.println();
}
}
複製代碼
作者:
孫焌仁
時間:
2019-1-24 14:54
import java.util.Arrays;
public class Ch10
{
public static void main(String args[])
{
int G[]={-163,65,-98,70,9,8591,9487,0,-11,-90,123};
System.out.println("排序前:");
for(int i=0;i<G.length;i++)
System.out.print(G[i]+" ");
Arrays.sort(G);
System.out.println("\n排序後:");
for(int i=0; i<G.length; i++)
System.out.print(G[i]+" ");
}
}
複製代碼
作者:
楊于暄
時間:
2019-1-24 14:54
import java.util.Arrays;
public class Ch25 {
public static void main(String[] args) {
int n[]={34,65,7,-3,6546,-564,0,-11,90,123};
System.out.print("排序前: ");
for (int i=0; i<n.length; i++)
System.out.print(n[i]+" ");
Arrays.sort(n);
System.out.println();
System.out.print("排序後: ");
for (int i=0;i<n.length; i++)
System.out.print(n[i]+" ");
}
}
複製代碼
作者:
楊貳鈞
時間:
2019-1-24 14:59
import java.util.Arrays;
public class Ch14
{
public static void main(String args[])
{
int n[]={34,65,7,-3,6546,-564,0,-11,90,123};
System.out.println("排序前:");
for (int i=0;i<n.length;i++)
System.out.print(n[i]+" ");
System.out.println();
Arrays.sort(n);
System.out.println("排序後:");
for(int i=0; i<n.length;i++)
System.out.print(n[i]+" ");
}
}
複製代碼
作者:
林育鋐
時間:
2019-1-24 15:00
import java.util.Arrays;
public class Ch11
{
public static void main(String args[])
{
int n[]={34,65,7,-3,6546,-564,0,-11,90,123};
System.out.print("排序前: ");
for(int i=0;i<n.length;i++)
{
System.out.print(n[i]+" ");
}
System.out.println();
Arrays.sort(n);
System.out.print("排序後: ");
for(int i=0;i<n.length;i++)
{
System.out.print(n[i]+" ");
}
}
}
複製代碼
作者:
趙一鳴
時間:
2019-1-24 15:01
import java.util.Arrays;
public class Ch14
{
public static void main(String[] args)
{
int n[]={65,34,6,4,2,354,5,43,-343,675};
System.out.print("排序前:");
for(int i=0;i<n.length;i++)
{
System.out.print(n[i]+" ");
}
Arrays.sort(n);
System.out.print("\n排序後:");
for(int i=0;i<n.length;i++)
{
System.out.print(n[i]+" ");
}
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2