2012-01-19 44 views
5

我應該創建一個數組並將數字從最小到最大排序。以下是我迄今爲止:使用java對從最小到最大的數組進行排序

public class bubbleSort { 

public static void sort (int [] arrayName){ 
    int temp; 
    for (int i = 0; i < arrayName.length-1; i++) 
    { 
     if(arrayName[i] > arrayName[i+1]) 
     { 
      temp=arrayName[i]; 
      arrayName[i]=arrayName[i+1]; 
      arrayName[i+1]=temp; 
      i=-1; 
     } 
    } 
} 

public static void main(String[] args) { 
    int [] arrayName = new int[10]; 
    for (int i = 0; i < arrayName.length; i++) { 
     arrayName[i] = (int)(Math.random()*100); 
    } 

    System.out.println(sort(arrayName)); 
} 
} 

我在哪裏,我想打印出來的最後一行得到一個錯誤。我究竟做錯了什麼?

+1

什麼是錯誤您收到? – kosa

+1

你的排序'函數'沒有返回任何東西 – Dan

回答

15

您的sort(int[] array)方法不會返回任何內容。它是無效的,因此你不能打印它的返回。

+0

我建議通過arrayName運行for-loop並打印出每個索引。 – Alex

+0

啊,所以我需要將它改爲int而不是void。我如何得到它返回數組的所有數字作爲反對溫度或其他個人價值的價值? – Brett

+0

你擁有它的方式很好,你的變量arrayName現在被排序。你只需要現在穿過它的值,然後將它們打印出來:) – Alex

3

您需要迭代數組並打印出每個值。你不能只是println(<數組>)。相反,請嘗試:

// sort the array 
sort(arrayName); 
for(int sortedValue : arrayName) 
    System.out.println(sortedValue); 

這將遍歷數組中的每個元素並將其打印出來。

您也可以使用commons-lang's ArrayUtils.toString()方法爲您自動完成此操作,但我假設由於這是一項家庭作業,您不能只使用外部庫爲您完成工作。

0

你需要改變你的排序方法 - 它什麼都不返回。

public static void適用於不返回任何內容的方法。試試這個:

public static int sort (int[] arrayname) 
0
public static int[ ] arraySortUp(int[ ] intArray) 
{ 
     int toSwap, indexOfSmallest = 0; 
     int i, j, smallest; 

     for(i = 0; i < intArray.length; i ++) 
     {    

      smallest = Integer.MAX_VALUE; 

      for(j = i; j < intArray.length; j ++) 
      { 
       if(intArray[ j ] < smallest) 
       { 
        smallest = intArray[ j ]; 
        indexOfSmallest = j; 
       }     
      } 

      toSwap = intArray[ i ]; 
      intArray[ i ] = smallest; 
      intArray[ indexOfSmallest ] = toSwap; 
     } 

     return intArray; 
}  
1

對於學習的目的寫自己的排序功能是好的,但對於生產代碼總是使用的Java API Arrays.sort

2

也許你可以使用lambdaj(download herewebsite),這庫是用於管理集合(..list,數組),下面的代碼是非常簡單和完美的作品非常強大:

import static ch.lambdaj.Lambda.on; 
import static ch.lambdaj.Lambda.DESCENDING; 
import static ch.lambdaj.Lambda.sort; 
import java.util.Arrays; 
import java.util.List; 

public class Test { 
    public static void main(String[] args) { 
     List<Integer> numberList = Arrays.asList(4,8,2,3,4,1,13,2,5); 

     List<Integer> sortedList = sort(numberList, on(Integer.class)); 
     System.out.println(sortedList); //shows ascending list 

     sortedList = sort(numberList, on(Integer.class), DESCENDING); 
     System.out.println(sortedList); //shows descending list 
    } 
} 

此代碼所示:

[1, 2, 2, 3, 4, 4, 5, 8, 13] 
[13, 8, 5, 4, 4, 3, 2, 2, 1] 

在一行中,您可以對列表進行排序,這是一個簡單的示例,但使用此庫可以解決更多問題。

sort(numberList, on(Integer.class)); 

您必須添加lambdaj-2.4.jar到您的項目。我希望這會有用。

注意:這將幫助您假設您可以替代您的代碼。

0

這是 「乾淨」 的方式來做到這一點(我認爲):

public static void main(String[] args) throws IOException { 

    int[] array = {1,4,2,8,4,7,5 /*put in the numbers you want to sort*/}; 

    Arrays.sort(array); /*You will need to import this function*/ 

    for (int i = 0; i < array.length; i++) { 
     System.out.println(array[i]); 
    } 

    } 

希望這有助於!

-2

創建與Java Extentation.ie(ArraySorting.java)文件,然後將代碼粘貼....

import java.io.*; 
import java.util.Arrays; 
import java.util.Scanner; 
public class ArraySorting 
{ 


public static void main(String args[]) 
{ 
     Scanner user_input=new Scanner(System.in); 

     System.out.println("enter Size elements..."); 
     int Size=user_input.nextInt(); 

     int[] a=new int[Size]; 
     System.out.println("Enter element Of an Array..."); 
     for(int j=0;j<Size;j++) 
     { 
      a[j]=user_input.nextInt(); 
     } 

     Arrays.sort(a);  
     for(int index=0;index<a.length;index++) 
     { 
      System.out.println(a[index]); 
     } 

} 

}

+0

我只是在猜測,但我認爲OP正試圖編寫自己的排序算法,而不是使用現成的解決方案。 – LordWilmore

+0

這是一個簡單的Java Coding Dude:/ ....嘗試這個鏈接.... http://stackoverflow.com/questions/8931977/sort-array-from-smallest-to-largest-using-java/ 39248098#39248098。 thankx --- –

+0

相當。我會想象這是一個正在學習如何編程的人,當他們對他們變得「簡單」時,他們應該像你一樣使用庫方法,但跳到答案部分從來不是學習的好方法 – LordWilmore

0

陣列,而不使用內置的功能在Java 排序.... ..just使新的文件unsing這個名字 - >(ArraySorting.java)..... 運行該項目並享受它!!!!!

import java.io.*; 
import java.util.Arrays; 
import java.util.Scanner; 
public class ArraySorting 
{ 
public static void main(String args[]) 
{ 
    int temp=0; 
    Scanner user_input=new Scanner(System.in); 
    System.out.println("enter Size elements..."); 
    int Size=user_input.nextInt(); 

    int[] a=new int[Size]; 
    System.out.println("Enter element Of an Array..."); 
    for(int j=0;j<Size;j++) 
    { 
     a[j]=user_input.nextInt(); 
    }  
    for(int index=0;index<a.length;index++) 
    { 
     for(int j=index+1;j<a.length;j++) 
     { 
      if(a[index] > a[j]) 
      { 
       temp = a[index]; 
       a[index] = a[j]; 
       a[j] = temp; 
      } 
     } 
    } 
    System.out.print("Output is:- "); 
    for(int i=0;i<a.length;i++) 
    { 
     System.out.println(a[i]); 
    } 

} 

}

相關問題