简体   繁体   English

String 方法按字母顺序从字符串数组中返回第一个字符串

[英]String method to return the first string from a string array by alphabetic order

I have a problem in understanding how the following code is executed.我在理解如何执行以下代码时遇到问题。 I am seeking an example for 1/2 executions.我正在寻找 1/2 处决的例子。

Code is:代码是:

public class StringArray {
  public static String getFirstString(String[] values) {
    if (values.length == 0) {
      return "";
    }

    String result = values[0];
    for (int i=1; i<values.length; i++) {
      if (result.compareTo(values[i]) > 0) {// i.e. result > values[i]
        result = values[i];
      }
    }
    return result;
  }

  public static void main(String[] args) {
    String[] nume= {"Andrei", "Andreea", "Andrea", 
                    "Marius", "Marcus", "Marcel", "Florin"};
    System.out.println(getFirstString(nume));
  }
}

Basically, is the first item processed?基本上,第一个项目是否已处理?

First is Andrei.首先是安德烈。

1.Andrei will get into the first If? 1.Andrei会进入第一个If? values.length should not be 7? values.length不应该是 7?

1.1 "value" being the reference of the parameter, should point to the array[] name which is given in the main method, right? 1.1 “value”作为参数的引用,应该指向main方法中给出的array[]名称,对吧?

Therefore, Andrei will be compared to Andreea, but from here, why is Andrei bigger than Andreea?因此,安德烈会被比作安德烈亚,但从这里来看,安德烈为什么比安德烈亚大? I have a hard time with the if (result.compareTo(values[i]) > 0) .我很难使用if (result.compareTo(values[i]) > 0)

The key element here: understanding the "contract" of the compareTo() method.这里的关键元素:理解compareTo()方法的“契约”。

Start by looking at the javadoc :从查看javadoc开始:

Compares two strings lexicographically.按字典顺序比较两个字符串。 The comparison is based on the Unicode value of each character in the strings.比较基于字符串中每个字符的 Unicode 值。 The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.此字符串 object 表示的字符序列按字典顺序与参数字符串表示的字符序列进行比较。 The result is a negative integer if this String object lexicographically precedes the argument string.如果此字符串 object 按字典顺序在参数字符串之前,则结果为负 integer。 The result is a positive integer if this String object lexicographically follows the argument string.如果此字符串 object 按字典顺序跟随参数字符串,则结果为正 integer。 The result is zero if the strings are equal;如果字符串相等,则结果为零; compareTo returns 0 exactly when the equals(Object) method would return true.当 equals(Object) 方法返回 true 时,compareTo 返回 0。

This is the definition of lexicographic ordering.这是字典顺序的定义。 If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both.如果两个字符串不同,则它们在某个索引处具有不同的字符,该索引是两个字符串的有效索引,或者它们的长度不同,或两者兼而有之。 If they have different characters at one or more index positions, let k be the smallest such index;如果它们在一个或多个索引位置具有不同的字符,则设 k 为此类索引中的最小; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string.然后,在 position k 处的字符具有较小值的字符串,由使用 < 运算符确定,按字典顺序位于另一个字符串之前。 In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:在这种情况下, compareTo 返回两个字符串中 position k 处的两个字符值的差值——即值:

this.charAt(k)-anotherString.charAt(k) this.charAt(k)-anotherString.charAt(k)

If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string.如果没有索引 position 不同,则较短的字符串按字典顺序排列在较长的字符串之前。 In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:在这种情况下, compareTo 返回字符串长度的差值——即值:

this.length()-anotherString.length() this.length()-anotherString.length()

No, Your table which contains all the name (nume) goes to the first "if", so, you have 7 String, the value of your table of String is 7. The compareTo() function compare the alphabetical value, so Andrei is bigger than Andreea because "i" is bigger than "e".不,包含所有名称(nume)的表转到第一个“if”,所以,你有 7 个字符串,你的字符串表的值为 7。 compareTo() function 比较字母值,所以 Andrei 是比 Andreea 大,因为“i”比“e”大。 So, for each value of your table, you compare it with a reference value.因此,对于表的每个值,您都将其与参考值进行比较。 If it's lower, you take this one in reference.如果它较低,您可以参考这个。 You do it with all String of your list and you return the reference that is to say the lower value in order to select the last Value and you return it.您使用列表中的所有字符串执行此操作,然后返回参考值,即较低的值,以便 select 最后一个值,然后返回它。 So, here you should return Andrea because he has the lowest value in the list.所以,在这里你应该返回 Andrea,因为他在列表中的值最低。 And result should be Andrei -> Andreea -> Andrea结果应该是 Andrei -> Andreea -> Andrea

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

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