如何使用Java中的排序算法? [英] How to use a sorting algorithm in Java?

查看:210
本文介绍了如何使用Java中的排序算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是向用户字符串的他想输入数字。然后,我会提示他右边的字符串。那么我应该打印蜇按字母顺序排列。我得到了大部分的任务完成了,只需要一个排序算法,如冒泡排序来完成它。这是我的code。

 进口java.io. *;
公共类排序
{


私有静态的BufferedReader标准输入=新的BufferedReader(新的InputStreamReader(System.in));

  公共静态无效的主要(字符串[]参数)抛出IOException异常
  {
     的System.out.println(有多少串你想进入?);
     INT stringCount =的Integer.parseInt(stdin.readLine());
     的String [] stringInput =新的String [stringCount]
     的for(int i = 0; I< stringCount;我++)
     {
         System.out.print(你能在此输入字符串:);
         stringInput [I] = stdin.readLine();
     }
     //现在我如何使用排序算法?
  }
}
 

解决方案

<一个href="http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#sort%28java.lang.Object%5B%5D%29"相对=nofollow> Arrays.sort()

My assignment is to ask a user for the number of strings he would like to input. Then i will prompt him to right the strings. Then i am supposed to print the stings in alphabetical order. I got most of the assignment done, just need a sorting algorithm such as Bubble sort to finish it. this is my code.

import java.io.*; 
public class sorting
{


private static BufferedReader stdin=new BufferedReader(new InputStreamReader( System.in));

  public static void main(String[] arguments) throws IOException
  {
     System.out.println("How many strings would you like to enter?");
     int stringCount = Integer.parseInt(stdin.readLine());
     String[] stringInput = new String[stringCount];
     for(int i = 0; i < stringCount; i++)
     {
         System.out.print("Could you enter the strings here: ");
         stringInput[i] = stdin.readLine();
     }  
     //Now how do i use a sorting algorithm?
  }
}

解决方案

Arrays.sort().

这篇关于如何使用Java中的排序算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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