简体   繁体   English

按字母顺序对字符串列表进行排序

[英]Sorting a list of Strings in Alphabetical order

OK, here is my problem. 好,这是我的问题。 I need to take a array of Strings and sort it alphabetically and then print out the first String: For example, a string of "Georgia, Florida, Alabama", It should print out Alabama. 我需要一个字符串数组,并按字母顺序对其进行排序,然后打印出第一个字符串:例如,字符串“ Georgia,Florida,Alabama”,它应该打印出阿拉巴马州。 The Strings are not submitted by the user, i have a file with a bunch of states that is inputed as an array. 字符串不是由用户提交的,我有一个带有一堆状态的文件,这些状态作为数组输入。

This is what I have: 这就是我所拥有的:

import java.io.*;
import java.util.*;
public class MinString
{
    private static final int SIZE = 10;
    public static void main(String[] args)
    {
            String[] list = new String[SIZE];
            int numItems;

            numItems = Initialize (list);
            System.out.println(numItems);
    }

    private static int Initialize (String[] list)
    {
      //post : List is initialized with all strings from file.

      String filename, stateInput;
      int i = 0, numItems = 0;
      try  {
            System.out.print("Input File : ");
            Scanner stdin = new Scanner(System.in);
            filename = stdin.nextLine();
            stdin = new Scanner(new File(filename));

            while ((stdin.hasNext()) && (i < list.length))
            {
                    stateInput = stdin.nextLine();
                    System.out.println("S = " + stateInput);
                    list[i] = stateInput;
                    i++;
            }
            numItems = i;
      }
      catch (IOException e)  {
            System.out.println(e.getMessage());
      }
      return numItems;
    }

    // Method FindMin goes here
 private static String FindMin (String[] list, numItems);
 ?????

} }

I'm not sure how to write this FindMin Method. 我不确定如何编写此FindMin方法。 I need to write FindMin so that it takes as input an array of size numItems of strings and returns to the calling function the minimum string. 我需要编写FindMin,以便它以字符串numItems大小的数组作为输入,并将最小字符串返回给调用函数。

Any ideas? 有任何想法吗?

Since this feels like a homework I will help you to help yourself: 由于这感觉像是一项家庭作业,因此我将帮助您自助:

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#compareTo(java.lang.String ) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#compareTo(java.lang.String

最简单的方法是:

return Collections.min(Arrays.asList(list));

The java.util.Arrays object contains a bunch of static methods for working with arrays. java.util.Arrays对象包含用于处理数组的一堆静态方法。 I think Arrays.sort would probably help you here. 我认为Arrays.sort可能会在这里为您提供帮助。 Since Strings implement the Comparable interface with an alphabetic ordering the sorted array should give you the information you need. 由于字符串以字母顺序实现Comparable接口,因此排序后的数组应会为您提供所需的信息。

只需使用Arrays.sort(list)即可对列表进行排序。

import java.util.*;
class Six
{
public static void main(String arg[])
{
String str[]=new String[5];
Scanner in=new Scanner(System.in);
System.out.println("Enter the element of array :");
for(int i=0;i<=4;i++)
{
str[i]=in.next();
}
Arrays.sort(str);
System.out.println("The first element after sorting is:");
System.out.println(str[0]);
}
}
private static String findMin(String[] list) {
    String minState = list[0]; 
    for(int i=1; i<list.length; i++){
        String min=list[i];
        minState=(min!=null&&min.compareTo(minState)<0)?min:minState;
    }
    return minState;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM