2012-01-19 45 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]); 
    } 

} 

}

相关问题