一步一步學習java數組學習(ch5)int數組排序

來源:互聯網
上載者:User
public class TestArgs{
public static void main(String[] args){
if(args.length<3){
System.out.println("usage java TestArgs \"n1\" \"op\"\"n2\"");
System.exit(-1);
}

double d1 = Double.parseDouble(args[0]);
double d2 = Double.parseDouble(args[2]);

double d = 0;
if(args[1].equals("+"))d = d1+d2;
else if(args[1].equals("-"))d = d1-d2;
else if(args[1].equals("x"))d = d1*d2;
else if(args[1].equals("/"))d = d1/d2;
else {
System.out.println("error");
System.exit(0);
}
System.out.println(d);
}
}

數組名 = new 數組元素類型[數組長度];

不可以初始化就定義數組大小。

剛開始學java, 現在正在一步一步 跟著視頻學習,感覺以後可能要回顧我自己的曆程,所以就再這裡記錄下來今天做了哪些事情。這樣以後或許可以回顧一下知識點。2011-11-15 14:08:15 

現在又持續看尚學堂的視頻

貼上代碼,原視頻只有選擇排序,自己又加了一個冒泡排序。

View Code

 1 public class TestSelectionSort{
2 public static void main(String[] args){
3 int[] a = new int[args.length];
4 for(int i=0;i<args.length;i++){
5 a[i] = Integer.parseInt(args[i]);
6 }
7
8 TestSelectionSort.print(a);
9 bubbleSort(a);
10 TestSelectionSort.print(a);
11
12 }
13
14 private static void print(int[] a){
15 for(int i=0;i<a.length;i++){
16 System.out.print(a[i]);
17 }
18 System.out.println();
19 }
20 //選擇法排序
21 private static void selectSort(int[] a){
22 for(int i =0;i<a.length;i++){
23 for(int j =i+1;j<a.length;j++){
24 if(a[i]>a[j]){
25 int temp;
26 temp = a[i];
27 a[i] = a[j];
28 a[j] = temp;
29 }
30 }
31 }
32 }
33 //冒泡法排序
34 private static void bubbleSort(int []a){
35 for(int i=0;i<a.length-1;i++){
36 for(int j=0;j<a.length-i-1;j++){
37 if(a[j+1]<a[j]){
38 int temp = 0;
39 temp = a[j];
40 a[j] = a[j+1];
41 a[j+1] = temp;
42 }
43 }
44 }
45 }
46 }

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.