2014-02-18 40 views
0

我試圖當我嘗試打印輸出不打印任何一個給定的使用插入sort.But數據的排序,適當的輸出是:插入排序調試

Albatross 1 
mockingbird 2 
vultures 3 
redwoodpeckers 6 
pigeons 7 
crows 10 
condos 12 
bluejays 15 
dodos 15 
baldeagles 25 
cardinals 40 
hummung birds 88 

String[]bn={"Cardinals","BlueJays","Albatross","Vultures","Crows","Mockingbirds","Condors","BaldEagles","Pigeons","RedHeadWoodPecker","Hummingbirds","Dodos"}; 
int[]bq={40,15,1,3,10,2,12,25,7,6,88,15}; 
public static void SortNumericalOrdernsert (String[] bn,int[] bq){ 
    for(int i=1;i<bq.length;i++){ 
     int next=bq[i]; 
     String y=bn[i]; 
     //find all the insertion location 
     //Move all the larger elements up 
     int j=i; 
     while(j>0 && bq[j-1]>next){ 
      bn[j]=bn[j-1]; 
      bq[j]=bq[j-1]; 
       j--; 
     } 
     //insert the element 
     bq[j]=next; 
     bn[j]=y; 
    } 

可以有人請求幫助

+2

你應該表現出你在哪裏打印陣列的代碼。 – Sumedh

+0

你在哪裏打印?我看不到任何'print'語句 – nachokk

+0

您能提供代碼,您如何調用SortNumericalOrdernsert方法。 –

回答

0

我複製並粘貼您的代碼,它運行正常我相信問題是打印方法。請提供它,以便我們找出問題所在。

我已經把你的運行代碼以及它沒有任何問題。我相信你應該提供整個班級,以便我們確定問題所在。這就是我添加你的代碼的方式,並使它的輸出與預期一致。

public class Sort { 

    static String[]bn={"Cardinals","BlueJays","Albatross","Vultures","Crows","Mockingbirds","Condors","BaldEagles","Pigeons","RedHeadWoodPecker","Hummingbirds","Dodos"}; 
    static int[]bq={40,15,1,3,10,2,12,25,7,6,88,15}; 

    public static void SortNumericalOrdernsert (String[] bn,int[] bq) { 
     for(int i=1;i<bq.length;i++){ 
      int next=bq[i]; 
      String y=bn[i]; 
      //find all the insertion location 
      //Move all the larger elements up 
      int j=i; 
      while(j>0 && bq[j-1]>next){ 
       bn[j]=bn[j-1]; 
       bq[j]=bq[j-1]; 
       j--; 
      } 
      //insert the element 
      bq[j]=next; 
      bn[j]=y; 
     } 
    } 

    public static void ShowAllBirds(String[]bn,int[]bq) { 
     for(int a=0;a<=bn.length-1;a++) { 
      System.out.println(bn[a]+" : "+bq[a]); 
     } 
    } 
    public static void main(String[] args) { 
     SortNumericalOrdernsert(bn, bq); 
     ShowAllBirds(bn, bq); 
    } 
} 
+0

公共靜態無效ShowAllBirds(字符串[] BN,INT [] BQ){ \t \t // TODO自動生成方法存根 \t \t對(INT A = 0;一<= bn.length-1;一++){ System.out.println(bn [a] +「:」+ bq [a]); – user3321128

0

你需要添加一個循環遍歷並打印陣列的各個元素:

for(String name: bn){ // this needs to be included at the end of your sort method AFTER the for loop 
    System.out.println(name); 
} 

被告知; Java是按值傳遞的,而不是通過引用,如果打算之後使用排序後的數組,則必須返回具有修改值的數組。

只要注意到你需要名稱和它的值;將無法使用增強for循環。你需要的東西一起去的線路:

有關循環和數組
for(int index = 0; index < bn.length; index++){ 
    System.out.println(bn[index] + " " + bq[index]); 
} 

更多信息可以在Java教程和文檔(只是谷歌他們)被發現。

0

它的工作原理,把arrray億和BQ作爲靜態

public class test { 

    /** 
    * @param args 
    */ 
    static String[]bn={"Cardinals","BlueJays","Albatross","Vultures","Crows","Mockingbirds","Condors","BaldEagles","Pigeons","RedHeadWoodPecker","Hummingbirds","Dodos"}; 
    static int[]bq={40,15,1,3,10,2,12,25,7,6,88,15}; 
    public static void SortNumericalOrdernsert (String[] bn,int[] bq){ 
     for(int i=1;i<bq.length;i++){ 
      int next=bq[i]; 
      String y=bn[i]; 
      //find all the insertion location 
      //Move all the larger elements up 
      int j=i; 
      while(j>0 && bq[j-1]>next){ 
       bn[j]=bn[j-1]; 
       bq[j]=bq[j-1]; 
        j--; 
      } 
      //insert the element 
      bq[j]=next; 
      bn[j]=y; 
     } 
    } 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     SortNumericalOrdernsert(bn,bq); 
     for(int i = 0; i< bn.length; i++) 
     { 
      System.out.println(bn[i] + "\t" + bq[i]); 
     } 
    } 
} 
+1

聲明不需要靜態的靜態變量是不好的編程習慣我已經被告知;特別是當你剛剛學習Java並需要學習範圍和封裝時。代碼有效,我只是不知道它是否正確。 –