java数组重大到小排序array,使用java将数组从最小到最大排序 (Sort array from smallest to largest using java)...

最佳答案

英文原文

You need to iterate over the array and print out each value. You cannot just println(). Instead, try:

// sort the array

sort(arrayName);

for( int sortedValue : arrayName )

System.out.println( sortedValue );

That will iterate over each element in the array and print it out.

You can also use commons-lang's ArrayUtils.toString() method to do this all automatically for you, but I am assuming that since this is a homework assignment, you cannot just use external libraries to do your work for you.

中文翻译

您需要遍历数组并打印出每个值。你不能只是println(< array>)。相反,尝试:

//对数组进行排序

排序(arrayName中);

for(int sortedValue:arrayName)

System.out.println(sortedValue);

这将迭代数组中的每个元素并将其打印出来。

你也可以使用commons-lang的ArrayUtils.toString()方法为你自动完成所有这些,但我假设因为这是一个家庭作业,你不能只使用外部图书馆为您工作。

You need to iterate over the array and print out each value. You cannot just println(). Instead, try:

// sort the array

sort(arrayName);

for( int sortedValue : arrayName )

System.out.println( sortedValue );

That will iterate over each element in the array and print it out.

You can also use commons-lang's ArrayUtils.toString() method to do this all automatically for you, but I am assuming that since this is a homework assignment, you cannot just use external libraries to do your work for you.

您需要遍历数组并打印出每个值。你不能只是println(< array>)。相反,尝试:

//对数组进行排序

排序(arrayName中);

for(int sortedValue:arrayName)

System.out.println(sortedValue);

这将迭代数组中的每个元素并将其打印出来。

你也可以使用commons-lang的ArrayUtils.toString()方法为你自动完成所有这些,但我假设因为这是一个家庭作业,你不能只使用外部图书馆为您工作。

参考答案2

Maybe you can use lambdaj (download here,website), this library is very powerfull for managing collections (..list,arrays), the following code is very simple and works perfectly:

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 numberList = Arrays.asList(4,8,2,3,4,1,13,2,5);

List 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

}

}

This code shows:

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

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

In one line you can sort a list, this is a simple example but with this library you can resolve more.

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

You must add lambdaj-2.4.jar to your project. I hope this will be useful.

Note: This will help you assuming you can have alternatives to your code.

参考答案3

Your sort(int[] array) method returns nothing. It is void, therefore you cannot print its return.

参考答案4

For learning purpose writing your own sort function is fine but for production code always use java API Arrays.sort

参考答案5

You need to change your sort method--it is returning nothing.

public static void is for a method where you are not returning anything. Try this:

public static int sort (int[] arrayname)

参考答案6

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;

}

参考答案7

This is the "cleaner" way to do this (I think):

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]);

}

}

Hope this help!

参考答案8

Array Sorting without using built in functions in java

......just make new File unsing this name -> (ArraySorting.java) .....

Run the Project and Enjoy it !!!!!

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

{

a[j]=user_input.nextInt();

}

for(int index=0;index

{

for(int j=index+1;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

{

System.out.println(a[i]);

}

}

}

参考答案9

Create File with java Extentation.i-e(ArraySorting.java) then Paste the Code....

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

{

a[j]=user_input.nextInt();

}

Arrays.sort(a);

for(int index=0;index

{

System.out.println(a[index]);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值