爪哇 - 订购数组值从最高到最低 [英] Java - Ordering array values from highest to lowest

查看:130
本文介绍了爪哇 - 订购数组值从最高到最低的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我要着手做Java中的方法各种出从阵列中所有的值到最高到最低。例如:如果我给这个数组:

So I want to go about making a method in Java that sorts out all values from an array into highest to lowest. For example: If I'm given this array:

int[] nums = {1254, 4875, 9452, 5412, 6245, 5158, 6215, 8426, 20158};

我怎么会提出这样打印出所有从最高这些数字到最低的方法?

How would I make a method that prints out all of these numbers from highest to lowest?

推荐答案

您应该通过排序数组开始:

You should begin by sorting the array:

Arrays.sort(nums);

这将在升序排序的数组,所以如果你只是通过循环回路落后,应该这样做:

This will sort the array in ascending order, so if you just loop backward through the loop, that should do it:

for(int i=nums.length-1; i>=0; i--){
      System.out.println(nums[i]);
}

希望帮助!

这篇关于爪哇 - 订购数组值从最高到最低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆